Why is it necessary to call `BroadcastChannel.send...
# coroutines
i
Why is it necessary to call
BroadcastChannel.send
in a suspended function? With e.g. RxJava pushing events to a subject doesn't require anything special.
l
@iex You are probably looking for
offer
.
i
ah, makes sense, thanks!
(just started using channels)
there should be something like
offer
, but which throws an error if the channel is full (instead of returning false)?
given the options I actually feel a bit safer using
runBlocking { observable.send(x) }
l
You can easily make your own if you want to throw if that's how you need to handle it. If it should never happen, you can for example do
check(myBroadcastChannel.offer(element))
You can also do
launch { myBroadcastChannel.offer(element) }
Or use
sendBlocking
if you're sure you need to block the sending thread.
👍 1
i
what would be the purpose of the one with
launch
?
l
To wait for the channel to no longer be full and send when possible
i
ah,
sendBlocking
is perfect!
that (the option with
launch
) would be identical to
runBlocking { observable.send(x) }
?
ah no
it's async
confused 🤔
l
But with launch and send, you're not blocking that expensive thread (expensive in terms of memory and that it cannot be used for something else). Blocking a thread when you don't need to is like holding a car that you don't need
It's like useless traffic lights… oh wait, I see a pattern here 😅
i
yeah, definitely. This is just to call some (non long running) callbacks on the main thread
so blocking is fine
thanks for the help, everything is clearer now 🙂
l
Well, depends on which platform… if you are in a UI environment (e.g. mobile/desktop app), it's absolutely not fine, at all. If it's on a server, it depends, but it might not be best to block thread
i
as I said, it's not long running. It's just to channel the control flow in a specific way.
in this specific case I've a component to start a permission flow, and when it finishes, it notifies other components
l
in UI environment, long running can start before 1ms long. It all adds up
It's in an Android app?
i
yes
l
I made this for Android permissions requests with coroutines which is very likely to be what you're looking for: https://github.com/LouisCAD/Splitties/tree/master/modules/permissions
i
thanks! I've already an rx component which I'm happy with and just changing it to use channels instead of subjects
but will give it a look if I need something new
l
I personally found Channels to be better hidden in implementation details for this kind of use case, simply exposing suspending functions as a higher level API instead.
But I'm glad I helped either way 🙂
i
I find observables convenient, because I can have arbitrary components observing the result and don't have to care about timing etc.
but I just like modeling everything with observables 🙂
l
In coroutines world, the analogue is Flow and suspending functions. It's very easy to turn one or more suspending functions and some logic into a Flow BTW.
👍 2
i
It's still needed to use
BroadcastChannel
for where I was using
PublishSubject
apparently
(or
ConflatedBroadcastChannel
for
BehaviorSubject
from what I just read)
l
If you care only about the latest value,
StateFlow
might be a great solution.
i
it's more that I need to push values to it arbitrarily
l
But on the receiving end?
You can just set a new value on a
MutableStateFlow
i
just sending... Ideally the interface to the observer would expose something different
(previously I used
Observable
)
but
BroadcastChannel
doesn't implement
Flow
, so I can't apply the same pattern
l
(I'm not very familiar with Rx FYI)
i
MutableStateFlow
-> interesting, will give that a look 👍
l
But you don't need a
BroadcastChannel
if you're using
MutableStateFlow
and expose
StateFlow
or a plain
Flow
.
i
I just googled the equivalents for the rx stuff and that's what came up 😅
l
It is, it's just going away soon, replaced by already avilable
StateFlow
, and upcoming
SharedFlow
.
i
good to know, thanks!
j
What's going away,
BroadcastChannel
? @louiscad
a
Very interesting thread
l
ConflatedBroadcastChannel
in favor of
StateFlow
.
BroadcastChannel
, I cannot tell. But they are still safe to use if
StateFlow
doesn't suit your use case.
a
and what will be the purpose of shared flow?
l
a
Thank you
While I totally understand what a
StateFlow
is, I still have partial knowledge on what a
SharedFlow
is (Ironic ey? provided that
StateFlow
implements
SharedFlow
)
Thanks any way.
l
There's two link to related issues in the PR I linked. Regardless, it'll be more clear when it'll be released with docs, and examples in the wild.
z
If
StateFlow
is like Rx's
BehaviorSubject
, then
SharedFlow
is basically like
PublishSubject
. However,
SharedFlow
actually supports propagating backpressure to the sender, like
BroadcastChannel
, so if one of your subscribers is slow, the send call will suspend.
SharedFlow
also supports custom replay behavior, so you can make it act like a
StateFlow
(but without access to the current value). Both flows are also unlike Subjects in that you can't close or fail them directly, although you can get similar behavior by applying operators to the flows before exposing them.
a
Woooow... thank you so much