Is there any nice class that can be used as a repl...
# coroutines
m
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
BroadcastChannel
m
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
ConflatedBroadcastChannel
keeps last message
If you create BroadcastChannel(BUFFERED), it will work
m
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
that's why you need to create buffered
m
aha, thanks ill test that
b
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
👍
a
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
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
If you're scoping a BroadcastChannel that narrowly you're probably in good shape
m
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
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
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
Agreed, it sounds like you have a well-scoped use case