Vlad
07/21/2020, 8:03 PMchannel.consumeEach { }
but not close channel on cancel. consumeEach does not close BroadcastChannel nor ConflatedBroadcastChannel on cancelation. Should i just stick to
for(it in channel){
}
and create my own helper extension function?octylFractal
07/21/2020, 8:05 PMconsumeEach
, but does not close on cancel", but also "`consumeEach` does not close on cancel", which makes it sound like you already have what you wantVlad
07/21/2020, 8:12 PMval channel = Channel<Int>(capacity = 1)
val broadcastChannel = BroadcastChannel<Int>(capacity = 1)
launch { channel.consumeEach { } }.apply {
delay(100)
cancel()
}
launch { broadcastChannel.consumeEach { } }.apply {
delay(100)
cancel()
}
println("${channel.isClosedForReceive} ${channel.isClosedForReceive}")
println("${broadcastChannel.isClosedForSend}")
output
false false
true
I want it to stay alive and use reuse it sometime lateroctylFractal
07/21/2020, 8:13 PMfor
loop? I think that's basically what you want, and it isn't much longer than a method calloctylFractal
07/21/2020, 8:14 PMconsumeEach
docs as well: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/consume-each.htmloctylFractal
07/21/2020, 8:14 PMIf you need to iterate over the channel without consuming it, a regularloop should be used instead.for