I'm having an issue with LiveData's
observeAsState()
and Flow's
collectAsState()
just not being invoked on change anymore, where
observe()
and
collect()
lambdas
are still being invoked on change.
ViewModel:
private val _spellLiveData: MutableLiveData<DataState<List<Spell>>> =
MutableLiveData(DataState.Loading)
val spellLiveData: LiveData<DataState<List<Spell>>> = _spellLiveData
...
// Posting initial Loading, Success states
viewModelScope.launch {
...
_spellLiveData.postValue(it)
}
...
// Posting new Success states with modified data
viewModelScope.launch {
...
_spellLiveData.postValue(it)
}
MainActivity:
// This onChanged listener will always be invoked after a change.
viewModel.spellLiveData.observe(owner = this, onChanged = {
Log.d(TAG, "onCreate: $it")
})
// This will stop being invoked after the initial Loading, then Success states.
// So we can't update it.
val spells by viewModel.spellLiveData.observeAsState(initial = DataState.Loading)
I'm fetching 308 DnD 5e Spells, each with a few paragraphs about how they're used, as well as some smaller fields. After posting the initial 308, I can't post another 308 in its place and have it update. But, when I post modified data with just 3 spells,
observeAsState()
and
collectAsState()
work fine, and the views recompose correctly. So I'm wondering if I've reached the limits of what state can do? I thought it might be a bug in
observeAsState()
, but the same problem occurs with Flow's
collectAsState()
.