![]() StateFlow always has a value which can be safely read at any time via value property. StateFlow is simpler, because it does not have to implement all the Channel APIs, which allows for faster, garbage-free implementation, unlike ConflatedBroadcastChannel implementation that allocates objects on each emitted value. It has the following important differences: StateFlow vs ConflatedBroadcastChannelĬonceptually, state flow is similar to ConflatedBroadcastChannel and is designed to completely replace it. Use SharedFlow when you need a StateFlow with tweaks in its behavior such as extra buffering, replaying more values, or omitting the initial value. A state flow behaves identically to a shared flow when it is created with the following parameters and the distinctUntilChanged operator is applied to it: State flow always has an initial value, replays one most recent value to new subscribers, does not buffer any more values, but keeps the last emitted one, and does not support resetRepla圜ache. See the SharedFlow documentation for the basic rules, constraints, and operators that are applicable to all shared flows. State flow is a special-purpose, high-performance, and efficient implementation of SharedFlow for the narrow, but widely used case of sharing a state. State flow behavior with classes that violate the contract for Any.equals is unspecified. It is used to conflate incoming updates to value in MutableStateFlow and to suppress emission of the values to collectors when new value is equal to the previously emitted one. Values in state flow are conflated using Any.equals comparison in a similar way to distinctUntilChanged operator. Derived values can be defined using various operators on the flows, with combine operator being especially useful to combine values from multiple state flows using arbitrary functions.įor example, the following class encapsulates an integer state and increments its value on each call to inc:Īs an alternative to the above usage with the MutableStateFlow(.) constructor function, any cold Flow can be converted to a state flow using the stateIn operator. StateFlow is useful as a data-model class to represent any kind of state. So a slow collector skips fast updates, but always collects the most recently emitted value. Updates to the value are always conflated. ![]() The value of mutable state flow can be updated by setting its value property. An active collector of a state flow is called a subscriber.Ī mutable state flow is created using MutableStateFlow(value) constructor function with the initial value. A call to llect on a state flow never completes normally, and neither does a coroutine started by the Flow.launchIn function. Its current value can be retrieved via the value property. A state flow is a hot flow because its active instance exists independently of the presence of collectors. Interface StateFlow : SharedFlow ( source)Ī SharedFlow that represents a read-only state with a single updatable data value that emits updates to the value to its collectors.
0 Comments
Leave a Reply. |