Are there any coroutine-friendly observer pattern ...
# coroutines
s
Are there any coroutine-friendly observer pattern implementations?
LiveData
is great but only for Android.
t
Flow?
r
More specifically, you might be looking for a StateFlow
g
To be honest, LiveData is not great for Android too
k
LiveData is great until you discover Flows 😆
s
@gildor so why is it part of the officially recommended app architecture?
g
Because they couldn't recommend a much more superior, but complex library like RxJava (which also requires another third party lib to work with Android) And now Flow is officially supported by all Androidx API, and thought LiveData is officially not deprecated or discouraged (also Flow will not work in Java anyway), I don't see reason to use it Nothing against LiveData itself if you consider that Android needed some simple and safe solution for observable properties, but I would use it only if would be no alternatives, which we do have and one of them is officially supported
s
In the google Android codelabs, the repository often returns a flow from the database, and the ViewModel returns
flow.toLiveData()
.
g
Well, codelab is just a codlab, unclear when it was updated and it may have own requirements LiveData used to have a couple unique useful semantics, like it stopped collection onStop lifecycle, but now there is official flowWithLifecycle for this, so there is no real difference. Nothing wrong with using LiveData, I just don't understand why
s
Good to know about flowWithLifecycle!
211 Views