https://kotlinlang.org logo
#coroutines
Title
# coroutines
z

zak.taccardi

10/24/2019, 5:46 PM
Let’s say I have a
CoroutineScope
backed by a multi-thread dispatcher. I also have a
Channel<Int>
that needs to receive incoming messages.
Copy code
val scope = CoroutineScope(Dispatchers.Default)
val channel: Channel<Int> = TODO()

fun onNewInt launch`(newInt: Int) {
  // `newInt` is delivered from outside a coroutine
  // what is the appropriate way to emit to the channel? 
}
I see two possibilities (see code snippet)
2️⃣ 1
1️⃣ 2
vote
1
or
2
for preferred approach
w

withoutclass

10/24/2019, 5:48 PM
Depending on your case of course, but
UNLIMITED
may not be what you want if you have slow consumers
maybe you want 500 items or something, that is if you went with option 2
o

octylFractal

10/24/2019, 5:49 PM
really depends on how you want the channel to operate. if you're aiming for an unlimited back-log, I would vote for 2, as it's more explicit about that. but if you want backpressure, I would set RENDEZVOUS/specific count, and use
sendBlocking
rather than offer
👍 1
g

gildor

10/24/2019, 11:14 PM
It's purely depends on semantics which you want
👍 1
z

zak.taccardi

10/24/2019, 11:15 PM
There's no guarantee on ordering though with
scope.launch { }
👌 1
3 Views