Does anyone have any tips of wrapping a `LiveData`...
# android
k
Does anyone have any tips of wrapping a
LiveData
in a
Flow
or using flow in instead of Livedata?
r
they are two different things. two different used cases
g
Not sure that those are different
There is no official adapter for LiveData <-> Flow
but I believe that we need one
âž• 3
but probably instead adding it to Koitlinx.coroutines it should be ktx library
👍 1
Probably make sense to create a feature request on Android Issue tracker
they already have some coroutines support, but only for liveData builder:
using flow in instead of Livedata
Using Flow instead of Livedata should be completely fine IMO, but some integration from Arch components will not work (like data binding for example)
s
Is there something in Flow that can hold the last value like behaviour subject?
v
I think you may be looking for coroutine channels
g
ConflatedBroadcastChannel is work like that
and you can just open Flow subscription to Channel, so from user point it will look as Flow
also there is proposal to reimplement ConflatedBroadcastChannel on top of Flow: https://github.com/Kotlin/kotlinx.coroutines/issues/1082
r
They are different in a sense , that one is for cold asynchronous streams and the other is just a data holder that can be lifecycle aware...
g
Observable data holder and Kotlinx.coroutines have own implementation of it (ConflatedBroadcastChannel). And also Flow is pretty safe in terms of lifecycle. In general I agree that the main use case for Flow is different, but still can be used for the same as LiveData
j
live data is like a last mile thing where errors can't happen
you really shouldn't use it for anything, but if you have to it needs to just be a bridge between the last gap of your presenter/controller/whatever and your UI
somewhat related, ktx libraries shouldn't depend on coroutines despite the fact that some of the existing ones already do. library integrations and ktx are two very different things
r
Tell that to viewModelScope lol. Well there is always that debate in whether Coroutines should be considered just a different library, or a language feature that just happens to be in a different library
j
yeah well... i don't use view models either so...
r
Lmaoo, yea I remember lol
But yea , everything has there own usecase. Corutines, are awesome in there own domain.. Just like rxjava is good in its domain.Same for livedata.. They are all different concepts... Nothing needs to replace anything.Just choose what fits the need for your codebase lol
g
I agree, it shlouldn’t be just livedata-ktx that provides this Flow adapter, but it may be livedata-coroutines-kts, same way as livedata-reactivestreams