In our (compose for desktop based) project we're u...
# compose
s
In our (compose for desktop based) project we're using a lot of
mutableStateOf()
and a background thread for potentially longer running tasks that deliver data and alter state variables. Naturally, sometimes buttons etc. also write to such state variables. Now we're wondering about thread safety and what kind of synchronization mechanisms we might need. I realize the state handling and snapshots do a lot of heavy lifting for us, but it would be really nice to read some documentation about that. I find next to nothing on that topic in the compose docs: https://developer.android.com/jetpack/compose/documentation We have found Zach's series "Compose state explained" https://dev.to/zachklipp/series/12895 and the articles there are very insightful. Still I'm wondering if there might be other "official" resources such as docs or references on those topics that we might have missed?
1
I have read multiple people who seem to know what they're saying here on the slack that writing to MutableState is thread-safe, which is good to hear. However my understanding is that if we do not use explicit snapshots anywhere, our multiple threads are operating in the global snapshot where the default change-merging strategy is that writes are non-conflicting, i.e. values written by one thread can be overwritten by a different one. So it appears to me, depending on the situation, there might still be a need for classical synchronization or maybe alternatively the use of explicit snapshots.
z
I think official docs on this are planned but don't exist yet. Writing to mutable states is definitely thread safe in low level terms, but if you're implementing more complex read/write operations then explicit snapshots would be the thing to use.
Snapshots are like transactions
s
thanks for your answer, alright it seems to me I really need to try out how things look if we introduce snapshots into our codebase
a
I was under the impression MutableState was not thread safe since when I try to set a value from a background coroutine that does initialization I get an exception like this:
Copy code
java.lang.IllegalStateException: Reading a state that was created after the snapshot was taken or in a snapshot that has not yet been applied
        at androidx.compose.runtime.snapshots.SnapshotKt.readError(Snapshot.kt:1527)
        at androidx.compose.runtime.snapshots.SnapshotKt.current(Snapshot.kt:1770)
        at androidx.compose.runtime.SnapshotMutableStateImpl.setValue(SnapshotState.kt:299)
        at com.zzz.PlaygroundViewModelImpl$1.invokeSuspend(PlaygroundViewModel.kt:178)
        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
though I guess this happens because my ViewModel is created on first usage from compose (which seems like a common pattern when using viewModel() helper)