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

aaverin

08/27/2018, 3:48 PM
At the moment I get an error that
send
can only be called in coroutine context, that is hidden by the callback function
v

Vsevolod Tolstopyatov [JB]

08/27/2018, 4:11 PM
We have
SendChannel<E>.sendBlocking
for this purpose
b

bj0

08/27/2018, 4:15 PM
or you can use
.offer
if you don't mind dropped events, or are using a conflated channel
a

aaverin

08/28/2018, 7:07 PM
@Vsevolod Tolstopyatov [JB] I tried using
sendBlocking
but get ClosedSendChannelException code is
Copy code
produce {
   addSnapshotListener { snapshot ->
     val list = parseSnapshot(snapshot)
     sendBlocking(list)
   }
}
Any ideas why is SendChannel already closed?
What I need is a channel wrapping callback that is executed on every dataset change
Just to clarify I have an active socket connection to a database and every change is delivered in a snapshot callback I need to wrap this callback with coroutine so I could actively subscribe from my client implementation
v

Vsevolod Tolstopyatov [JB]

08/28/2018, 8:01 PM
SendChannel is closed because you either have closed it or
produce
block ended (e.g. if
addSnapshotListener
is asynchronous)
a

aaverin

08/28/2018, 8:02 PM
addSnapshotListener
is asynchronous, that’s kind of the point of the code I am trying to write Once in a while I will be getting updates from server socket, and I need to expose them as a channel consumers could subscribe to
To get a single value from socket I am using
suspendCancelableCoroutine
I am trying to find an equivalent in Channels world so I could keep listening the socket and get all updates
b

bj0

08/28/2018, 8:47 PM
you need something to prevent the
produce
block from suspending, or just use a normal channel
a

aaverin

08/29/2018, 5:29 AM
I don’t mind
produce
suspending, after all it’s an async operation I am wrapping. Problem is that
send(data)
can only be executed within coroutine body
Still digging through docs and articles trying to figure out what I missed
b

bj0

08/29/2018, 5:59 AM
ah what i meant to say was "prevent the
produce
block from *ending*", in other words, you need to suspend it somehow
produce
ends -> channel closes
3 Views