fabiocollini
10/02/2018, 6:33 AMproduce
method to create a channel inside a suspending method but I have some problems. I have a different behaviour defining my method as CoroutineScope
extension method and using coroutineScope
inside a suspending method. These are the two versions:
private fun CoroutineScope.produceExtension(): ReceiveChannel<Int> = produce {
send(1)
send(2)
send(3)
}
private suspend fun produceCoroutineScope(): ReceiveChannel<Int> = coroutineScope {
produce {
send(1)
send(2)
send(3)
}
}
`
Using the second method I am not able to use the channel (the method never ends), I have already opened an issue with the complete code here https://github.com/Kotlin/kotlinx.coroutines/issues/645
Am I doing something wrong? Should the two methods be equivalent?gildor
10/02/2018, 6:35 AMfabiocollini
10/02/2018, 6:36 AMgildor
10/02/2018, 6:38 AMfabiocollini
10/02/2018, 6:42 AMproduce
method defined in CoroutineScope
gildor
10/02/2018, 6:43 AMfabiocollini
10/02/2018, 6:48 AMgildor
10/02/2018, 6:49 AMfabiocollini
10/02/2018, 6:49 AMcoroutineScope
method another valid way of doing it?kingsley
10/02/2018, 6:58 AMcoroutineScope
will suspend until the producer is closed, which doesn’t happen in this example 🤔
Anyway though, you could define it as extension function in another class and execute like so:
with(anotherClassInstance) {
produceExtension()
}
assuming this is already within a coroutine scopefabiocollini
10/02/2018, 7:00 AMclose
invocation I have the same problem (and I’d like to consume the values before the channel is closed)gildor
10/02/2018, 7:03 AMfabiocollini
10/02/2018, 7:10 AMwith
method until they define the integration of channels with structured concurrency