StateFlow does not emit an updated object value if...
# coroutines
t
StateFlow does not emit an updated object value if a property of the object is changed, is this the intentional design? Example:
Copy code
data class Count(var value: Int = 0)

private val count = MutableStateFlow(Count())

val newCount = count.value
newCount.value++ // no emission though value is updated
count.value = newCount // this does not emit either
👌 4
z
Yes. Updating the property doesn't emit because the flow has no way of knowing anything happened. Then sending the same object back into the flow doesn't emit because the flow now sees that the new value is equal to the old value. In general, reactive streams work best with immutable "value" objects.
t
Okay thanks Zac
m
Beyond that,
StateFlow
uses content equality (
==
) and only emits values when the object contents differ. See "Strong equality-based conflation" in the
StateFlow
docs: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/index.html See also this sample demonstrating the effect: https://klassbook.commonsware.com/lessons/Flows%20and%20Channels/stateflow-equality.html
👍 1
t
Thanks Mark
192 Views