I have a
LiveData
of type
MutableLiveData<MutableList<User>>
.
I have an issue.
If I update the
value
by another
MutableList<User>
the changes are emitted to the observers.
However, if I simply add other elements to the
MutableList<User>
, that is already initialised in the
LiveData
, the changes are not emitted.
Here is what I am doing...
//other codes
private val _users: MutableLiveData<MutableList<User>> = MutableLiveData<MutableList<User>>()
init {
_users.value = mutableListOf<User>()
}
val uiScope = CoroutineScope(Dispatchers.Main + Job())
fun loadUsers(){
uiScope.launch{
// this works perfectly
_users.value = fetchUsers() as MutableList<User>
// this doesn't work... no idea why
_users.value.addAll(fetchUsers())
}
}
suspend fun fetchUsers(): List<User>{
return withContext(<http://Dispatchers.IO|Dispatchers.IO>){
//fetch users asynchronously
}
}
This is just an emulation of the actual program. That actual code is not that trivial.