class SomeClass{
val list : SnapshotStateList<Int> = mutableStateListOf(5)
}
val someClass = SomeClass()
snapshotFlow { someClass.list }.onEach { println(it.toString()) }.collectIn(scope)
the onEach block is not executed despite mutating the list
I must instead explicitly do
snapshotFlow { someClass.list.toList() }
I am kind of confused why this is the case, and I feel like this must be a pitfall Im likely to run into again. Is this the intended behaviour? Could someone explain why?
a
Albert Chang
09/24/2021, 1:06 PM
snapshotFlow
only emits a new value when a value read in the lambda changes. The list instance itself never changes. Its values change, but you are not reading any of its values in the lambda, so the flow won’t emit new values.
toList()
function reads the values of the original list so using that will work.