https://kotlinlang.org logo
Title
i

iex

07/12/2020, 5:24 PM
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

louiscad

07/12/2020, 5:28 PM
@iex You are probably looking for
offer
.
i

iex

07/12/2020, 5:30 PM
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

louiscad

07/12/2020, 5:34 PM
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

iex

07/12/2020, 5:35 PM
what would be the purpose of the one with
launch
?
l

louiscad

07/12/2020, 5:35 PM
To wait for the channel to no longer be full and send when possible
i

iex

07/12/2020, 5:35 PM
ah,
sendBlocking
is perfect!
that (the option with
launch
) would be identical to
runBlocking { observable.send(x) }
?
ah no
it's async
confused 🤔
l

louiscad

07/12/2020, 5:37 PM
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

iex

07/12/2020, 5:38 PM
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

louiscad

07/12/2020, 5:40 PM
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

iex

07/12/2020, 5:40 PM
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

louiscad

07/12/2020, 5:42 PM
in UI environment, long running can start before 1ms long. It all adds up
It's in an Android app?
i

iex

07/12/2020, 5:42 PM
yes
l

louiscad

07/12/2020, 5:43 PM
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

iex

07/12/2020, 5:46 PM
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

louiscad

07/12/2020, 5:47 PM
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

iex

07/12/2020, 5:49 PM
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

louiscad

07/12/2020, 6:14 PM
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

iex

07/12/2020, 6:57 PM
It's still needed to use
BroadcastChannel
for where I was using
PublishSubject
apparently
(or
ConflatedBroadcastChannel
for
BehaviorSubject
from what I just read)
l

louiscad

07/12/2020, 6:58 PM
If you care only about the latest value,
StateFlow
might be a great solution.
i

iex

07/12/2020, 6:58 PM
it's more that I need to push values to it arbitrarily
l

louiscad

07/12/2020, 6:59 PM
But on the receiving end?
You can just set a new value on a
MutableStateFlow
i

iex

07/12/2020, 6:59 PM
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

louiscad

07/12/2020, 7:00 PM
(I'm not very familiar with Rx FYI)
i

iex

07/12/2020, 7:00 PM
MutableStateFlow
-> interesting, will give that a look 👍
l

louiscad

07/12/2020, 7:01 PM
But you don't need a
BroadcastChannel
if you're using
MutableStateFlow
and expose
StateFlow
or a plain
Flow
.
i

iex

07/12/2020, 7:01 PM
I just googled the equivalents for the rx stuff and that's what came up 😅
l

louiscad

07/12/2020, 7:01 PM
It is, it's just going away soon, replaced by already avilable
StateFlow
, and upcoming
SharedFlow
.
i

iex

07/12/2020, 7:33 PM
good to know, thanks!
j

julian

07/12/2020, 9:59 PM
What's going away,
BroadcastChannel
? @louiscad
a

andylamax

07/13/2020, 12:07 AM
Very interesting thread
l

louiscad

07/13/2020, 7:16 AM
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

andylamax

07/13/2020, 9:29 AM
and what will be the purpose of shared flow?
l

louiscad

07/13/2020, 9:30 AM
a

andylamax

07/13/2020, 9:31 AM
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

louiscad

07/13/2020, 9:51 AM
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

Zach Klippenstein (he/him) [MOD]

07/13/2020, 2:06 PM
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

andylamax

07/13/2020, 4:30 PM
Woooow... thank you so much