Alex
10/13/2020, 9:53 AMMutableStateFlow
? I have been using ConflatedBroadcastChannel
, but would like to move to flow completely. I know there is Flow
itself, but I have not found a way to send values to it from outside the creation lambda.KamilH
10/13/2020, 9:59 AMMutableStateFlow
as Flow
? The former one implements the latter, so you can do it by just castingMarc Knaup
10/13/2020, 10:13 AMgildor
10/13/2020, 10:24 AMMarc Knaup
10/13/2020, 10:31 AMnull
is not a valid value.
In that case you could make your own val undefined = Any()
and filter that out.Alex
10/13/2020, 11:45 AMFlow
?Marc Knaup
10/13/2020, 11:46 AMMutableStateFlow
note that here is another difference between using a Channel and a hidden `StateFlow`:
StateFlow
is conflating, i.e. it drops intermediate results if the collector is slow.Alex
10/13/2020, 11:47 AMConflatedBroadcastChannel
and then calling .asFlow()
would still be my best bet. Maybe I can wrap that in a version of AbstractFlow
and call it MutableFlow
to keep with the definitions.Marc Knaup
10/13/2020, 11:49 AMAlex
10/13/2020, 11:51 AMMutableStateFlow
works perfectly fine, it’s a fantastic addition. But there are some things that should not be retained as states but still emitted (usually called effects), for example Toast messages (small messages shown once). For this a non-stateful Flow
works, but it has to be able to be triggered from the outside.
If you used MutableStateFlow
for effects then you would show a toast message and then upon phone rotation would show the same toast message again, which is not required.AbstractFlow
is actually only for stateful Flow
implementations:
Base class for stateful implementations of `Flow`.
private lateinit var effectsEmitter: FlowCollector<Effect>
val effects = flow<Effect> {
effectsEmitter = this
}
Marc Knaup
10/13/2020, 11:55 AMFlowCollector
from the wrong coroutine context and likely raise an exception.effectsEmitter
. So if you have multiple collectors for the flow they break each other.Alex
10/13/2020, 11:57 AMFlow
private val _effects = ConflatedBroadcastChannel<Effect>()
val effects: Flow<Effect> = _effects.asFlow()
Marc Knaup
10/13/2020, 12:02 PMAlex
10/13/2020, 12:57 PMMarc Knaup
10/13/2020, 12:59 PMAlex
10/13/2020, 1:56 PMConflatedBroadcastChannel
in the example above with?gildor
10/13/2020, 2:11 PMMarc Knaup
10/13/2020, 2:13 PMgildor
10/13/2020, 2:13 PM