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

kevin.cianfarini

07/03/2019, 12:02 AM
Does anyone have any tips of wrapping a
LiveData
in a
Flow
or using flow in instead of Livedata?
r

rkeazor

07/03/2019, 1:34 AM
they are two different things. two different used cases
g

gildor

07/03/2019, 3:12 AM
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

Saiedmomen

07/03/2019, 4:05 AM
Is there something in Flow that can hold the last value like behaviour subject?
v

voben

07/03/2019, 4:11 AM
I think you may be looking for coroutine channels
g

gildor

07/03/2019, 5:10 AM
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

rkeazor

07/03/2019, 12:53 PM
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

gildor

07/03/2019, 1:15 PM
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

jw

07/03/2019, 11:52 PM
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

rkeazor

07/04/2019, 12:26 AM
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

jw

07/04/2019, 12:30 AM
yeah well... i don't use view models either so...
r

rkeazor

07/04/2019, 12:31 AM
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

gildor

07/04/2019, 1:48 AM
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