I couldn't understand something about callbackFlow
. I have two implementation. There is only difference both of them is using delay
or any other suspend function which includes delay
. When I run 1. implementation, awaitClose
block doesn't run but there is no problem when run the 2. implementation. Coroutine was canceled when I call the first
function and in my code implementation needs first
call.
I believe there is a logical explanation about that but I couldn't solve it.
val callbackFlow = callbackFlow {
send("Value")
delay(100)
awaitClose { println("Channel was closed") }
}
val firstValue = callbackFlow.first()
println(firstValue)
val callbackFlow = callbackFlow {
send("Value")
awaitClose { println("Channel was closed") }
}
val firstValue = callbackFlow.first()
println(firstValue)
Flow.first() cancels the flow once the first value has been collected.
In your case, it means that the awaitClose
function is never reached.
callbackFlow.first()
triggers flow collectionsend("value")
transmit value to the collectorNote however, I'm not sure that the awaitClose
is always reachable, even without delay. More research would be needed to give a definitive answer for that.