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

Mikael Alfredsson

03/12/2020, 8:45 AM
Is there any nice class that can be used as a replacement for android LocalBroadcastManager? like a conflated channel but without any “memory” not even the last sent message?
b

bezrukov

03/12/2020, 8:51 AM
BroadcastChannel
m

Mikael Alfredsson

03/12/2020, 8:53 AM
I thought that one also kept the last message? So if i send something and no-one is listening, i still think i got that message when I started to listen later on that channel.
I might be wrong since I tried so many different things 🙂
b

bezrukov

03/12/2020, 9:02 AM
ConflatedBroadcastChannel
keeps last message
If you create BroadcastChannel(BUFFERED), it will work
m

Mikael Alfredsson

03/12/2020, 9:03 AM
Copy code
public fun <E> BroadcastChannel(capacity: Int): BroadcastChannel<E> =
    when (capacity) {
        0 -> throw IllegalArgumentException("Unsupported 0 capacity for BroadcastChannel")
        UNLIMITED -> throw IllegalArgumentException("Unsupported UNLIMITED capacity for BroadcastChannel")
        CONFLATED -> ConflatedBroadcastChannel()
        BUFFERED -> ArrayBroadcastChannel(CHANNEL_DEFAULT_CAPACITY)
        else -> ArrayBroadcastChannel(capacity)
    }
oh sorry for the formatting, Creating a BroadcastChannel with the size Conflated just creates a conflated broadcast channel.
b

bezrukov

03/12/2020, 9:04 AM
that's why you need to create buffered
m

Mikael Alfredsson

03/12/2020, 9:04 AM
aha, thanks ill test that
b

bezrukov

03/12/2020, 9:05 AM
Copy code
when `capacity` positive, but less than [UNLIMITED] -- creates `ArrayBroadcastChannel` with a buffer of given capacity.
 *   **Note:** this channel looses all items that are send to it until the first subscriber appears;
m

Mikael Alfredsson

03/12/2020, 9:05 AM
👍
a

Adam Powell

03/12/2020, 1:55 PM
Note that the reason
LocalBroadcastManager
is deprecated is because the concept of a single application-wide event bus for unrelated, unscoped events leads to code that is difficult to understand and maintain. Using
BroadcastChannel
to implement the same idea will have the same associated issues.
👆 2
Splitting out different kinds of events into their own channels and scoping access and lifetime of those channels will produce much cleaner code than a literal translation from
LocalBroadcastManager
🙂
m

Mikael Alfredsson

03/12/2020, 1:59 PM
Interesting. My current usage was to get “system wide” exceptions (like user changed password on another device, thus has to be logged out from this device as well) from a network interceptor over to the current activity in order to push the user to the login screen.
a

Adam Powell

03/12/2020, 2:01 PM
If you're scoping a BroadcastChannel that narrowly you're probably in good shape
m

Mikael Alfredsson

03/12/2020, 2:01 PM
Ok, thats seems to be more or less what i want to do, i.e. one channel for one type of events, but I want to have events not state, thats why i wanted to mimic the broadcasts with the “if non are listening, the event just dissapears”
👍 1
Still, I could probably implement this with some kind of states instead of events, and use the conflated channel types instead.
a

Adam Powell

03/12/2020, 2:02 PM
LBM and event bus usage often grew into a single bus handling those kinds of events, plus all UI button clicks, plus impression tracking, etc
m

Mikael Alfredsson

03/12/2020, 2:03 PM
it’s way to easy to fall in that trap, but for single purpose, it seems like this could be a nice way forward.
a

Adam Powell

03/12/2020, 2:04 PM
Agreed, it sounds like you have a well-scoped use case
34 Views