在将Koltin Flow与LiveData一起使用时未调用onCompletion

时间:2020-01-23 14:19:10

标签: android kotlin android-livedata coroutine flow

因此,这就是我尝试使用Flow的过程,我在ProgressBar中显示onStart,并试图在ProgressBar中隐藏onCompletion

在ViewModel类中appDatabase.eventDao().getAllEvents()返回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)
}

一切正常,我无法弄清楚为什么在任务完成时没有调用onCompletion

0 个答案:

没有答案