'channel or mutablesharedflow , which one is a better replacement for deprecated localbroadcastmanager

In the past, I was using LocalBroadcastManager and EventBus in my chat and taxicab applications which are now either deprecated or not recommended using them.

I intend to replace them with new data structures like mutablesharedflow or channel, i want to know which one is better matched with my case? or maybe another data structure?



Solution 1:[1]

From Roman Elizarov, Channels were added as an inter-coroutine communication primitive.

You cannot use channels to distribute events or state updates in a way that allows multiple subscribers to independently receive and react upon them.

So they introduced Flow. But Flow is a cold observable, where every subscriber gets their own data (independent from other subscribers). With SharedFlow you get a hot observable that emits independently of having any subscriber(s).

You could do the same with ConflatedBroadcastChannel. But JetBrains recommend to use Flow in favor of Channels because of their simpler API.

So if you want to migrate to Coroutines and you need a hot observable multiple subscribers can listen to, you should go with SharedFlow.

Solution 2:[2]

Both are good for listening one time events. Everything depends in your use cases.

SharedFlow known as hot flow ->

  1. Emit events even if no observer is listening to it
  2. If no observer is listening to it, you loose these events

Channels known as cold flow

  1. Does not emit events when no observer is not listening to it. It works like a BlockingQueue.
  2. When you start to collect, it collects all data which were sent.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChristianB
Solution 2 Veton Neziri