I am bit confused on what Coroutine flow should I ...
# coroutines
k
I am bit confused on what Coroutine flow should I take to replace LiveData. Flow, StateFlow, SharedFlow?
a
StateFlow is a good substitute for LiveData
🤔 1
e
Note that there are a few important differences. Some of which are listed on the Android developers documentation site. Are you looking for similar behaviour of the live data? Or on lifecycle events?
k
I am looking for the Flow type that can carry my network result to the compose function, the one that is most appropriate 😄 @Erik
l
SharedFlow if there's no initial value, StateFlow otherwise.
e
There are many more differences wrt LiveData, especially regarding behaviour
a
SharedFlow and LiveData are not substitutes for one another. SharedFlow does not hold a current value to emit upon subscription by multiple consumers over time.
l
@Adam Powell except if its replay is set to 1.
a
which isn't useful if you're trying to replace a usage of LiveData and intend to consume it from Compose. LiveData supporting an uninitialized state conflated with a current null value (and its generally loose treatment of nullability) was a design flaw. Carrying that forward by using SharedFlow with a replay is just going to lead to confusion in the app data flow.
l
SharedFlow doesn't conflate the fact that the initial value isn't there yet, even when the type is nullable, since the replay is also a list that can be accessed, and can be empty. Agreed for Compose, getting a StateFlow that represents at least loading and empty without ambiguity is best. Bonus if you can represent cached data, and its freshness, plus estimates of loading time to expect.