简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM