https://kotlinlang.org logo
Title
w

william

10/24/2020, 4:16 PM
StateFlow
appears to compare values before updating them (unlike
LiveData
), so if I have a mutable list which I update and send to my
MutableStateFlow
it doesn't get published from the looks of it - unless I create a new list / copy it. Is that the right way to work around this?
a

Adam Powell

10/24/2020, 4:22 PM
Probably yes; consider kotlinx.immutable lists if you find you need to optimize for the costs of copying your particular data set.
👍 1
Generally speaking, passing mutable data through flows that you continue to mutate after emitting is risky and can lead to problems with multithreading
w

william

10/24/2020, 4:23 PM
yeah for sure. i don't mutate it anyway but its probably good practice regardless
a

Adam Powell

10/24/2020, 4:29 PM
if you're changing the same collection instance before emitting it again, you definitely are mutating it 🙂
those changes will be made out from under any downstream collectors that may have retained that reference for later use
and since many flow operator use async channels as implementation details, it is quite possible for the upstream emit call to complete before the downstream collector begins processing that emitted value
w

william

10/24/2020, 4:31 PM
yeah sorry - i just meant the 'observer' isn't mutating it. but yeah i see
good thoughts though i will check this out more