Will editing 2 mutableState values "at the same time" result in 2 recompositions?
Zoltan Demant
11/05/2021, 2:42 PM
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
}
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
Zach Klippenstein (he/him) [MOD]
11/05/2021, 4:54 PM
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
George Mount
11/05/2021, 7:50 PM
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
Zoltan Demant
11/06/2021, 10:10 AM
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!