```Snapshot.withMutableSnapshot { itemList.forEac...
# compose
t
Copy code
Snapshot.withMutableSnapshot {
 itemList.forEach { item -> _messagesStateMap[someIndex] = item }
}
Would this be a proper usecase for Snapshot? I dont want to trigger UI Update on every iteration of the loop So, with this, I guess it’ll only trigger update after the
apply
is called. What do you guys think?
a
The behavior here depends on the thread you’re running that code on. If you’re running on the main thread, the whole loop will run before any UI updates are triggered, so the
Snapshot.withMutableSnapshot
is probably not needed. If you’re on a background thread instead, that story changes a bit and
Snapshot.withMutableSnapshot
would help ensure what you mentioned. This thread has some previous discussion in this area: https://kotlinlang.slack.com/archives/CJLTWPH7S/p1641896888250700
z
More specifically, if you’re running this on a background thread, there’s a race condition where this can trigger multiple UI updates iff vsync triggers a recomposition while your loop is running. That’s probably unlikely to happen often, but certainly possible. Recomposition doesn’t get invalidated on every state change, and it doesn’t run more than once a frame no matter how many times it’s invalidated for that frame. If you can guarantee this is running on the UI thread, the extra snapshot is not worth the overhead.
t
Yep. This will be run on the main thread. So, I don’t think this will be needed. Thank you very much for the information!!