Is there some kind of `blockingProduce`? I have an...
# coroutines
a
Is there some kind of
blockingProduce
? I have an async callback that I want to wrap into a channel
d
why you need blocking? just use ordinary
produce
a
it closes before my callback sends any data
d
How many times is your callback called by the async function?
Once or n times?
d
it closes before my callback sends any data
just make a loop with suspension point inside
a
I have 3rd party library that would deliver data in async manner into a callback any time data changes in the database
So I need a channel that would deliver me those updates forever while app is running
d
I don't think produce should be used then. In a new function just create a channel and send to the channel inside your callback, then return the channel.
a
produce is the producing part of the channel, isnt’ it?
something that only sends, but doesn’t receive
d
Yes but it won't help you here.
Since your function is non-blocking but not suspending.
a
should I use
suspendCoroutine
that would publish into a channel?
d
I was just thinking about that but if you use
suspendCoroutine
, you can only call the
Continuation
once, as supposed to multiple times, which is what you need.
Copy code
fun magicAsync(): ReceiveChannel<Item> {
        val ch = Channel(...)
        magic(..., { it -> ch.send(it) })
        return ch
}
👍 1
a
Yes, looks like it’s the only way
I’ll try applying it, thanks