So here is what I was trying to do with Flow
, I am showing a ProgressBar
in onStart
and trying to hide the ProgressBar
in onCompletion
.
In ViewModel class appDatabase.eventDao().getAllEvents()
returns Flow<List<EntityEvents>
@ExperimentalCoroutinesApi
val allEvents: LiveData<Outcome<List<Event>>> = _fetchEvents.switchMap { _ ->
appDatabase.eventDao().getAllEvents()
.map { eventListMapper.map(it) }
.map { sortEventsBasedOnPreference(it) }
.flowOn(Dispatchers.IO)
.map { Outcome.success(it) }
.onStart { emitLoading(true) }
.onCompletion { emitLoading(false) }
.catch { emitFailure(it, R.string.err_something_wrong) }
.asLiveData(context = viewModelScope.coroutineContext)
}
All working fine, what I am not able to figure out why is onCompletion
not called when the task is completed?
if appDatabase.eventDao().getAllEvents()
is based Room on Flow, never called onCompletion()
.
Why?
Because getAllXXX()
Query is 'Hot'.
Actually, query is not completed. Only data is emited.
When the data changes, the query will emit data again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With