'No succeeding emission after catching an exception in Flow/LiveData
I'm loading some data using Flow which works as expected. The problem is, if an exception is thrown (handled in a <T> Flow<T>.catch), the succeeding call to loadData() is not received by someChannel.asFlow().onEach { print(it) }. On the other hand, the emission from the channel is still received by terminal flow operatorcollect()declared ininit` block. Do I miss something in handling the exception?
// Fragment
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
viewModel.loadData()
viewModel.apply {
someLiveData.observe(viewLifecycleOwner, Observer { data ->
adapter.refreshData(data)
})
}
}
// ViewModel
private val someChannel = ConflatedBroadcastChannel<Long>()
val someLiveData = someChannel.asFlow()
.onEach { print(it) } // no succeeding emission received
.flatMapLatest { repo.getData() }
.onStart { /* set values */ }
.onEach { /* set values */ }
.catch { t ->
// this properly catch the exceptions from the repository
/* set values */
_errorMessage.value = t.message
}
.asLiveData()
init {
viewModelScope.launch {
someChannel.asFlow()
.collect {
// This one still works even after catching the exception from `someLiveData` stream
print(it)
}
}
}
fun loadData() {
someChannel.offer(System.currentTimeMillis())
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
