Will editing 2 mutableState values "at the same ti...
# compose
z
Will editing 2 mutableState values "at the same time" result in 2 recompositions?
I think I read that this isnt the case, can someone verify? 🙂
Copy code
private var progress by mutableStateOf(0f)

private var label by mutableStateOf("")

fun update(
    progress: Float,
    label: String
) {
    this.progress = progress
    this.label = label
}
y
My naive understanding is that the state snapshots are the way to avoid this risk if using background threads https://dev.to/zachklipp/introduction-to-the-compose-snapshot-system-19cn -> "Multithreading and the global snapshot"
e
I think if it's on the main thread it won't, and if it's not you can do (don't remember if that's the correct function name but it's something like that)
Copy code
Snapshot.withMutableState {
  // update your state here
}
z
I’m pretty sure it won’t even if the mutations happen on a background thread, as long as both mutations happen before the next frame (for some definition of “next frame” that is whenever Compose decides to call
Snapshot.sendApplyNotifications
and schedule the next composition). But yea better to just use
withMutableState
to be certain.
g
As long as they are both edited in the same Snapshot, they'll be composed together. There is one open to allow developers to not explicitly start a Snapshot. If you want to be explicit about them happening at the same time, you can manage your own Snapshot, and commit it when you're done with all the changes that you want to apply.
z
Thanks for all the replies; I tried logging the recompositions and found just 1 happening for each update call, since I probably wont keep tabs on this forever I wrapped it
Snapshot.withMutableSnapshot
just to ensure no regressions happen down the line!