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

rkeazor

01/23/2020, 7:13 AM
Hey does LiveData<T>.asFlow create a hot flow?
l

louiscad

01/23/2020, 7:47 AM
No. The flow gets activated only when the LiveData if observed. There's also timeout of 5 seconds by default, and the last value of kept for future observers, even after the timeout.
r

rkeazor

01/23/2020, 8:48 AM
Wow so the livedata still needs a observer. Are you sure?
I thought when you call collect it puts it in a inactive state
Can the delay be remove or reduced
l

louiscad

01/23/2020, 10:25 AM
Oh sorry, I read it the other way around, you're converting LiveData to Flow! THe LiveData will be observed while the Flow is collected. So the Flow will not be hot until you call a terminal operator (like collect or something using it). The source code can confirm all the details, you should be able to navigate to it from IDE.
j

Jason

01/24/2020, 2:43 AM
Btw, what is difference between
hot flow
and
cold flow
?
l

louiscad

01/24/2020, 7:59 AM
@Jason A Flow is always cold by design (except it's being collected, but it's only hot for that collector). Here, Robert Keazor was talking about the implementations details of the
asFlow()
extension for
LiveData
in AndroidX Lifecycle LiveData KTX, and if the LiveData would be already observed before the resulting flow is collected, but it's not.
r

rkeazor

01/24/2020, 1:02 PM
Flow is cold . The simplest way I can explain it , is that cold has a designated start or end, usually at the call site. Like Observable.Just(1,2,3) is cold, because you start it and you know it ends after those 3 numbers are pushed out. But hot can work indefinitely outside the callsite. A simple example of this is View.onClickListener, because you can initialize it in any method but it will continue to work indefinitely until you remove the listener ... That's why Channels and sockets are considered hot by design
Oh ok I figured @louiscad wasnt sure. Once you change the livedata to a flow as soon as you call collect the livedata will be in its active state regardless of whether or not it has a observer or not.the lifecycle of the coroutine
12 Views