OK, now this drives me nuts. Why doesn't it print ...
# coroutines
a
OK, now this drives me nuts. Why doesn't it print anything and deadlocks at
flow.emit(1)
?
Copy code
fun main() = runBlocking {
    val flow = MutableSharedFlow<Int>()
    
    launch(<http://Dispatchers.IO|Dispatchers.IO>) {
        flow.onEach { println(it) }.first()
    }

    flow.emit(1)
}
e
the launched coroutine is still running so the parent scope is kept alive
a
I know it's running, but why doesn't it print
1
?
e
the emit happens before the collector starts
which means it's dropped
a
aaahhh
e
Copy code
launch(<http://Dispatchers.IO|Dispatchers.IO>, start = CoroutineStart.UNDISPATCHED) {
    flow.onEach { println(it) }.first()
}
will happen to wait for the collector to suspend before
launch
proceeds
(has other potential side-effects so it's not always the right thing to do, but in this case…)
a
you are totally right
OMG, I guess it's getting too late
Thanks a lot 🍻