Daniele Segato
06/30/2021, 10:59 PMMutableSharedFlow<Something?>(
replay = 1,
extraBufferCapacity = 1,
onBufferOverflow = BufferOverflow.DROP_OLDEST
)
vs
MutableStateFlow<Something?>(null)
Dominaezzz
07/01/2021, 7:43 AMDaniele Segato
07/01/2021, 7:44 AMDominaezzz
07/01/2021, 7:52 AMDaniele Segato
07/01/2021, 7:52 AMMutableSharedFlow
and why :)Nick Allen
07/03/2021, 7:11 PMMutableSharedFlow
maintains a buffer of size replay + extraBufferCapacity
(it's "extra" for a reason".
MutableStateFlow
has additional behavior in that it discards duplicates.
A MutableStateFlow
is equivalent to MutableSharedFlow(replay = 1, extraBufferCapacity = 0, onBufferOverflow = BufferOverflow.DROP_OLDEST)
, where you always read from the shared flow with sharedFlow.distinctUntilChanged()
.MutableStateFlow
is good for managing state that can change, MutableSharedFlow
is better suited for broadcasting event objects, where collectors are essentially event listeners. If something happens twice, you don't necessarily want the second event to be thrown away. The "event" in this case could be a sealed class of different event types, or could just be Unit to act as a simple signal.Daniele Segato
07/03/2021, 7:45 PM