https://kotlinlang.org logo
#coroutines
Title
# coroutines
k

K Merle

08/13/2021, 6:49 AM
I am bit confused on what Coroutine flow should I take to replace LiveData. Flow, StateFlow, SharedFlow?
a

andylamax

08/13/2021, 9:39 AM
StateFlow is a good substitute for LiveData
🤔 1
e

Erik

08/13/2021, 12:10 PM
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

K Merle

08/13/2021, 12:22 PM
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

louiscad

08/13/2021, 1:02 PM
SharedFlow if there's no initial value, StateFlow otherwise.
e

Erik

08/13/2021, 1:07 PM
There are many more differences wrt LiveData, especially regarding behaviour
a

Adam Powell

08/13/2021, 3:22 PM
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

louiscad

08/13/2021, 3:33 PM
@Adam Powell except if its replay is set to 1.
a

Adam Powell

08/13/2021, 3:39 PM
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

louiscad

08/13/2021, 9:37 PM
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.
3 Views