At the moment I get an error that `send` can only ...
# coroutines
a
At the moment I get an error that
send
can only be called in coroutine context, that is hidden by the callback function
v
We have
SendChannel<E>.sendBlocking
for this purpose
b
or you can use
.offer
if you don't mind dropped events, or are using a conflated channel
a
@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
SendChannel is closed because you either have closed it or
produce
block ended (e.g. if
addSnapshotListener
is asynchronous)
a
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
you need something to prevent the
produce
block from suspending, or just use a normal channel
a
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
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