Are there any use cases for using Channels in Andr...
# coroutines
l
Are there any use cases for using Channels in Android Development, or are you exclusively using Flows in your apps?
l
We're using channels for one time events, as a replacement for SingleLiveEvent
SharedFlow isn't a viable replacement as if an event is emitted when there's no subscribers it will be dropped
l
@luke_c Hmm, but Google says we shouldn't use channels for one time events: https://medium.com/androiddevelopers/viewmodel-one-off-event-antipatterns-16a1da869b95
l
In practice it's very unlikely it's going to be a problem... But I agree with the principal of handling events as state like the article mentions
l
Okay, and as far as I understood, events can only stay unprocessed if
send()
and/or
collect()
are called on a different Dispatcher than
Dispatchers.Main.immediate
, which shouldn't be the case that often since
lifecycleScope
and
viewModelScope
use this dispatcher by default, right?
l
Yes that's my understanding. It's important you use a buffered Channel so any events sent when there are no subscribers are buffered and not dropped
l
Okay, by default they have a capacity of 0, so we have to set it to a value >0, correct?
l
We use Channel.BUFFERED One caveat is that Channels have a fan out behaviour, so it's not reliable if you need multiple observers, but that's generally not a problem for most people and the same limitation that SingleLiveEvent has