abbic
04/04/2023, 9:46 AMvar mutableStateOf { UiState(etc) }
private set
my question is, is updating this state via
viewModelScope.launch {
// is on main thread here
uiState = uiState.copy(
multiple params set here
)
}
atomic, as long as its run on the main thread? found some guidance that suggests using Snapshot.withMutableSnapshot
to make compose state updates atomic, but the example there is updating multiple mutableStates
at once, not just one like in my exampleFilip Wiesner
04/04/2023, 10:06 AMabbic
04/04/2023, 10:07 AMviewModelScope.launch
i do a similar write to uiState, theres no risk of a race conditionabbic
04/04/2023, 10:07 AMwithContext(IO)
or somethingFilip Wiesner
04/04/2023, 10:08 AMvar
in Kotlin. If you are running on the same thread, than atomicity should not be a problem I thinkabbic
04/04/2023, 10:09 AMFilip Wiesner
04/04/2023, 10:16 AMwithMutableSnapshot
the "atomicity" means that all of the multiple changes of state inside that snapshot will be perceived by observers as one "atomic" change. It still wouldn't save you from race conditions If you are applying multiple snapshots to one variable.Filip Wiesner
04/04/2023, 10:16 AMFilip Wiesner
04/04/2023, 10:21 AMZach Klippenstein (he/him) [MOD]
04/04/2023, 6:36 PMZach Klippenstein (he/him) [MOD]
04/04/2023, 6:37 PMabbic
04/04/2023, 7:01 PM