Vinícius Santos
03/17/2023, 6:22 PMval state = MutableStateFlow("a")
val stateList = mutableList()
val job = launch { state.collect(stateLost::add) }
state.update {"b"}
state.update {"c"}
job.cancel()
//Expected
["a", "b", "c"]
//What actually happens
["a", "c"]
But if i add s small delay i got the expected result.
state.update {"b"}
delay(1)
state.update {"c"}
["a", "b", "c"]
ephemient
03/17/2023, 6:32 PMStateFlow
? states are always conflated - if multiple states are set before the collectors run, they only see the latest valueVinícius Santos
03/17/2023, 6:41 PMstate.update{ it.copy(newValue) }
Kevin Worth
03/17/2023, 7:01 PMupdate
function - you don’t need to worry about accidentally getting “c” first and then “b” becomes the latest (in a multithreaded situation more complicated than your simple example). So, given your description of ViewModel + Compose, it sounds to my like you’re good to go and don’t need to worry. But, again, if you want to be notified of each and every value - no matter how fast they come, then you definitely don’t want StateFlow
.update
call will be running (effectively) “bValue.copy(cValue)”Vinícius Santos
03/17/2023, 7:49 PM