Mini
09/24/2021, 12:27 PMclass 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?Albert Chang
09/24/2021, 1:06 PMsnapshotFlow
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.Adam Powell
09/24/2021, 1:54 PMsnapshotFlow { someClass.list.toString() }.onEach { println(it) }
is likely to be closer to what you want here, since the resulting string is what changes as part of the snapshot mutationsAdam Powell
09/24/2021, 1:55 PMMini
09/24/2021, 1:56 PM