Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onCompletion not called while using Koltin Flow with LiveData

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?

like image 218
Hussain Avatar asked Oct 16 '25 04:10

Hussain


1 Answers

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.

like image 116
최봉재 Avatar answered Oct 18 '25 17:10

최봉재



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!