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

Lukas Lechner

07/25/2022, 3:12 PM
To the Android Developers: Flow VS LiveData What is the advantage of exposing Flows instead of LiveData from your ViewModel to your Views? One argument I keep hearing is that you can then use flow operators in your View. However, since Views should actually only render received states to the screen and not have actual logic in them, I don't see any use case where I would need a flow operator there... I asked this on twitter as well: https://twitter.com/LukasLechnerDev/status/1551584399485026307
m

mkrussel

07/25/2022, 3:16 PM
The main benefits I go with is that it keeps more Android specific classes out of the view model and makes the view model easier to test.
👍 1
☝️ 1
a

Adam Powell

07/25/2022, 3:28 PM
LiveData only exists because at the time, Rx was too large of a library to suggest for basic use cases. Flow has a much gentler onramp for learning and kotlinx.coroutines is much smaller. By comparison to Flow, LiveData has a much more restrictive threading model, the tie to
Lifecycle
as opposed to the scoping of a suspend call is more complex than needed, and the hand-wavey nullability of LiveData during its initial uninitialized state leads to bugs and poor data modeling. There aren't any reasons to choose LiveData over Flow in new code beyond existing familiarity.
💯 1
👍 1
r

Robert Williams

07/25/2022, 3:37 PM
You are correct about not transforming Flows in Views, but if you have Flows coming from upstream it’s a lot simpler to manipulate them to return Flow then trying to do the same in LiveData