Marc Knaup
08/26/2019, 5:09 PMLiveData
as `Flow`:
@UseExperimental(ExperimentalCoroutinesApi::class)
fun <Value> LiveData<Value>.asFlow() = channelFlow<Value> {
val observer = Observer(this::offer)
observeForever(observer)
awaitClose { removeObserver(observer) }
}
Dominaezzz
08/26/2019, 5:17 PMlaunch
and send
, consider using offer
.Marc Knaup
08/26/2019, 5:19 PMDominaezzz
08/26/2019, 5:20 PMawaitClose
after observeForever
. Otherwise the observer will be registered after the Flow
closes/completes.Marc Knaup
08/26/2019, 5:21 PMAdam Powell
08/26/2019, 6:49 PMlouiscad
08/27/2019, 7:53 AMconflate()
.Marc Knaup
08/27/2019, 2:36 PMemit()
suspends until the value has actually been set.
If I use conflate()
for the other way round with Flow
the behavior would be different, wouldn’t it?Marc Knaup
08/27/2019, 2:37 PMchannelFlow
is already buffered 🤔Marc Knaup
08/27/2019, 2:50 PMLiveData
, not into it 🙄Marc Knaup
08/27/2019, 2:51 PM@UseExperimental(ExperimentalCoroutinesApi::class)
fun <Value> LiveData<Value>.asFlow() = channelFlow<Value> {
val observer = Observer(this::offer)
observeForever(observer)
awaitClose { removeObserver(observer) }
}.conflate()