SharedFlow and StateFlow
Learn about shared flow and how to use it.
Since Flow
is typically cold, its values are calculated on demand. However, there are cases in which we want multiple receivers to be subscribed to one source of changes. This is where we use SharedFlow, which is conceptually similar to a mailing list. We also have StateFlow, which is identical to an observable value. Let’s explain them both step by step.
MutableSharedFlow
Let’s start with MutableSharedFlow
, which is like a broadcast channel: everyone can send (emit) messages, which will be received by every coroutine that is listening (collecting).
Get hands-on with 1200+ tech skills courses.