Is there a common use case for using channels for ...
# coroutines
l
Is there a common use case for using channels for Android Development? I am only aware of using them for Single UI Events; however, Google recommends not using them for this purpose.
b
Well channels are just "hot" fliws, so my reasoning is always this - use channels here you'd normally use flows, but only when fliws are not "hot" enough for the use-case.
But I struggle to think of an actual example. Maybe some distributed background task that needs to run in a "daemon-like" manner?
j
probably channels can be used with some features like bluetooth
but not sure if shared flow can replace the old implementations I saw which were using channels tbh
s
Channels are not quite like hot Flows A hot Flow can be shared and can have multiple collectors (consumers) and every collector will get the emitted value. When a Channel's producer sends a value, only the first consumer gets the sent value (the consumer first in row that is awaiting a value). The others get nothing.
t
Doesn't that leave the question open a bit? If Google doesn't recommend channels for single ui events are hot shared flows the recommended alternative or are people anyway using channels and then turn them into flows after receiving the result for a specific one time UI event? Is there a recommended way?
s
Channels are good for handling onetime events. But SharedFlows are good for that as well (StateFlows are not good for this 😁). Most of the time, use a SharedFlow. Only if you do need specific Channel behavior, use Channels.
l
Hi @Timo Obereder 😃. Google recommends neither using Channels nor SharedFlows, but instead modeling events as state.