https://kotlinlang.org logo
#coroutines
Title
# coroutines
t

tjohnn

07/18/2020, 4:37 AM
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

Zach Klippenstein (he/him) [MOD]

07/18/2020, 4:49 AM
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

tjohnn

07/18/2020, 6:00 AM
Okay thanks Zac
m

Mark Murphy

07/18/2020, 11:41 AM
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

tjohnn

07/18/2020, 4:13 PM
Thanks Mark
136 Views