stateflow-flow-sharedflow-livedata
Basic app to use different type of observables StateFlow, Flow, SharedFlow, LiveData, State, Channel...
StateFlow, Flow, SharedFlow, LiveData
LiveData
LiveData is an lifecycle aware observable data holder ( means it knows the lifecycle of the activity or an fragment) use it when you play with UI elements(views).
StateFlow
StateFlow(hot stream) does similar things like LiveData but it is made using flow by kotlin guys and only difference compare to LiveData is its not lifecycle aware but this is also been solved using repeatOnLifecycle api's, So whatever LiveData can do StateFlow can do much better with power of flow's api. StateFlow won't emit same value.
Flow
Flow (cold stream) - In general think of it like a stream of data flowing in a pipe with both ends having a producer and consumer running on a coroutine.
SharedFlow
SharedFlow(hot stream) - name itself says it is shared, this flow can be shared by multiple consumers, I mean if multiple collect calls happening on the sharedflow there will be a single flow which will get shared across all the consumers unlike normal flow.
Channel
SharedFlow as a hot flow will emit data even if no one listening. Channel will hold data till someone consumes it. So if your view not ready to receive an event and sharedFlow emits it, the event will be lost. So channels are better to send one time events. Obviously you can set reply count for shared flow, but then your event will be repeated .
Tech stack:
- Dependency Injection (Hilt)
- Kotlin Coroutines
- Flow
- StateFlow
- SharedFlow
- LiveData
- ViewModel