> PreexistingSnapshotId > ```const val Preex...
# compose
m
PreexistingSnapshotId
Copy code
const val PreexistingSnapshotId = 1: Int
All new state objects initial state records should be
PreexistingSnapshotId
which then allows snapshots outside the creating snapshot to access the object with its initial state.
I don’t understand this explanation in the docs. Could anybody please help me make sense of it? Why is it
1
, and how does it achieve its objective?
s
The snapshot system is initialized with id=1 and then increments from there. StateRecord access logic has a special check for this id when no records are found for the current set of snapshots. This is only used for initial value of the state, so you can create it inside of the snapshot (e.g. during composition) and then access it globally
🙏 1
c
The theory here is that newly created `mutableStateOf`are considered to have always existed with their default values. That is,
val foo =mutableStateOf(27)
it assume to have always have existed with the value 27, the snapshot is just seeing it now. This is implemented by reserving a snapshot ID to hold the initial value of a mutable state, which is what
PreexsitingSnasphotId
is. This rather subtle change we made allows state to be created in a snapshot that escapes the snapshot and still valid to read or update even before the snapshot that created it applies. This can be used by background threads and background coroutines to perform optimistic work in anticipation of a composition successfully landing (e.g. loading a bitmap).