uli
05/24/2022, 6:24 AMinline fun <reified T> Flow<T>.collectIn(scope: CoroutineScope, collector: FlowCollector<T>): Job {
return scope.launch {
collect(collector)
}
}
It is so obvious, that I was thinking there must be something fundamentally broken if it is not part of the coroutines library.stojan
05/24/2022, 7:25 AMlaunhcIn
?
https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/launch-in.htmluli
05/24/2022, 7:42 AMJoffrey
05/24/2022, 7:45 AMlaunchIn
is terminal. Usually this is done via `onEach`+ launchIn
.uli
05/24/2022, 7:54 AMflow
.map {
...
}
.collectIn(scope) { value ->
consume(value)
}
stojan
05/24/2022, 7:58 AMlaunchIn
, there is nothing wrong with it.
The KotlinX Coroutines lib typically goes with minimal APIs, so if you need a function like the one you mentioned, you can write it yourself for your projectuli
05/24/2022, 8:01 AMis there any thing bad with a collectIn extension on Flow?
And explained:
It is so obvious, that I was thinking there must be something fundamentally broken if it is not part of the coroutines library.
Adam Powell
05/24/2022, 12:57 PMfoo.collect {
doStuff(it)
}
bar.collect {
doOtherStuff(it)
}
and they assume the above runs the two collections concurrently rather than sequentially. Reinforcing that assumption from Rx prolongs that confusionNick Allen
05/24/2022, 11:37 PMonEach
mistaken for a terminal operator as often as I've seen collect
mistaken for a concurrent non-suspending operator. Even knowing the API, It's easy to miss the launchIn
just as basically a typo (which is what led me to always creating my own versions of collectIn
operators).
It's also odd since from my experience, collect
is rarely useful. Most `Flow`s that I use never complete. StateFlow
, SharedFlow
, callbackFlow
wrapping addListener/removeListener APIs, Room generated `Flow`s. 99% of the time I use my own collectIn
. collect
is front and center in the docs, but in practice for me, it's been something of a fringe use-case only really useful in creating my own custom operators here and there.