abbic
08/12/2024, 3:56 PMstate.update {
it.mapIndexed { index, it ->
if (index == change index) ... else it
}
}
which seems wrongabbic
08/12/2024, 3:58 PMit.toMutableList().let { do my operations }.toList()
it feels a little less cursed but almost equally hackyZach Klippenstein (he/him) [MOD]
08/12/2024, 4:01 PMabbic
08/12/2024, 4:03 PMZach Klippenstein (he/him) [MOD]
08/12/2024, 4:05 PMIan Lake
08/12/2024, 4:06 PMabbic
08/12/2024, 4:06 PMabbic
08/12/2024, 4:06 PMabbic
08/12/2024, 4:07 PMabbic
08/12/2024, 4:08 PMIan Lake
08/12/2024, 4:08 PMIan Lake
08/12/2024, 4:09 PMabbic
08/12/2024, 4:10 PMColton Idle
08/12/2024, 5:12 PMColton Idle
08/12/2024, 5:12 PMAlex Vanyo
08/12/2024, 5:17 PMMutableStateFlow
Zach Klippenstein (he/him) [MOD]
08/12/2024, 5:20 PMcollectAsState()
, but you could also collect it in a LaunchedEffect
and update your own states, or write to a state in some other glue code.abbic
08/12/2024, 5:36 PMZach Klippenstein (he/him) [MOD]
08/12/2024, 5:37 PMColton Idle
08/12/2024, 6:41 PMIan Lake
08/12/2024, 6:48 PMColton Idle
08/12/2024, 7:05 PMabbic
08/13/2024, 8:25 AMabbic
08/13/2024, 8:26 AMabbic
08/13/2024, 8:27 AMZach Klippenstein (he/him) [MOD]
08/13/2024, 2:37 PMabbic
08/13/2024, 2:46 PMvar uiState by mutableStateOf(UiState())
...
viewModelScope.launch {
launch {
...
uiState = uiState.copy(data)
}
launch {
...
uiState = uiState.copy(otherData)
}
}
sometimes one of these writes would be overwrittenabbic
08/13/2024, 2:47 PMuiState.update { ... }
instead would remove this problemColton Idle
08/13/2024, 3:30 PMZach Klippenstein (he/him) [MOD]
08/13/2024, 5:31 PMuiState = uiState.copy(value)
is equivalent to doing the following with `MutableStateFlow`:
uiState.value = uiState.value.copy(value)
which is also not atomic.Zach Klippenstein (he/him) [MOD]
08/13/2024, 5:32 PMupdate {}
function is what makes the MutableStateFlow
update atomic. For snapshot state, you'd need to wrap the operation in a `withMutableSnapshot`:
launch {
Snapshot.withMutableSnapshot {
uiState = uiState.copy(data)
}
}
Zach Klippenstein (he/him) [MOD]
08/13/2024, 5:34 PMwithMutableSnapshot
block, you can write to as many state objects as you like, whereas with MutableStateFlow.update{}
you can only update that one flow only.abbic
08/13/2024, 5:38 PMZach Klippenstein (he/him) [MOD]
08/13/2024, 5:45 PMwithMutableSnapshot
is mentioned in the official documentation, but it's not used in most samples because you don't need it in most cases, especially when you're doing all your state updates on the main/ui thread.Zach Klippenstein (he/him) [MOD]
08/13/2024, 5:46 PMAlex Vanyo
08/13/2024, 5:46 PMZach Klippenstein (he/him) [MOD]
08/13/2024, 5:47 PMColton Idle
08/18/2024, 12:24 AM