Search code examples
androidkotlinchannelkotlin-sharedflow

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

  • 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.