Hi, I’m exploring the developments in Channels, Fl...
# coroutines
a
Hi, I’m exploring the developments in Channels, Flow, SharedFlow, StateFlow, etc. Could anyone please guide me on whether channels are still required in Android, or if Flows are sufficient for all use-cases? Also, some blogs, videos, guides with updated content would be very useful to understand in depth. Thank you. gratitude thank you
s
c
Flows are not a replacement of channels. Channels serve a different use case, and are the building blocks upon which many flow operators are built. Channels and cold flows are fundamentally different, but the hot SharedFlow is the most similar in concept to a Channel. The big distinction there is when multiple collectors are reading values from the same SharedFlow/Channel. A channel ensures each value is only sent to a single collector, so it's used for distributing the workload evenly. A SharedFlow sends each value to all collectors, so it's used for processing each value in different ways by each collector. Also, quite significantly, a SharedFlow cannot be "closed", while a Channel can be closed. The main purpose of a Channel is facilitating communication between two or more coroutines. A SharedFlow's purpose is in its name, sharing values, and isn't as good as general communication.
thank you color 1
p
SharedFlow is most similar to the deprecated BroadcastChannel which was a channel specialized for sending the value to all consumers
g
I would add that you probably shouldn't use Channels directly if you don't have a really good reason and know what you are doing Flow API is safer (structured concurrency), in most cases more performant So answering your question, yes Flow should be sufficient for the vast majority of cases
3