State Flow and Shared Flow - devrath/KotlinAlchemy GitHub Wiki
Important points
- Flows are mostly cold but
state-flow and shared-flow are exceptions here
- The
St.Flow and Sh.Flow exist independently of collectors. This means that these flows remain active even if there are no collectors.
State Flow is a special case of shared flow.
Shared Flow
- This is a flow that acts independently of collectors - Meaning the
share flow is active regardless of whether there are any collectors or not.
- Shared flow emits a
current state and new state to the subscribers.
Shared-flow never completes and remains active. Even though all the subscribers go away, The Shared Flow remains active.
State Flow
- State Flow is a version of Shared Flow.
- It has a single immutable value.
- It emits current and new states to the subscribers.
- It never completes
- It has a value property and we can retrieve the current value by using that value property.
State Flow is similar to LiveData
- It acts as a data holder and observable
LiveData is bound to the UI and the Android but the StateFlow is platform independent.