MutableSnapshot


A snapshot of the values return by mutable states and other state objects. All state object will have the same value in the snapshot as they had when the snapshot was created unless they are explicitly changed in the snapshot.

To enter a snapshot call enter. The snapshot is the current snapshot as returned by currentSnapshot until the control returns from the lambda (or until a nested enter is called. All state objects will return the values associated with this snapshot, locally in the thread, until enter returns. All other threads are unaffected.

All changes made in a MutableSnapshot are snapshot isolated from all other snapshots and their changes can only be seen globally, or by new shots, after MutableSnapshot.apply as been called.

Snapshots can be nested by calling takeNestedSnapshot or MutableSnapshot.takeNestedMutableSnapshot.

Summary

Public functions

open SnapshotApplyResult

Apply the changes made to state objects in this snapshot to the global state, or to the parent snapshot if this is a nested mutable snapshot.

Cmn
open Unit

Dispose the snapshot.

Cmn
open Boolean

Whether there are any pending changes in this snapshot.

Cmn
open MutableSnapshot
takeNestedMutableSnapshot(
    readObserver: ((Any) -> Unit)?,
    writeObserver: ((Any) -> Unit)?
)

Take a mutable snapshot of the state values in this snapshot.

Cmn
open Snapshot
takeNestedSnapshot(readObserver: ((Any) -> Unit)?)

Take a snapshot of the state values in this snapshot.

Cmn

Public properties

open Boolean

True if any change to a state object in this snapshot will throw.

Cmn
open Snapshot

The root snapshot for this snapshot.

Cmn

Inherited functions

From androidx.compose.runtime.snapshots.Snapshot
inline T
<T : Any?> enter(block: () -> T)

Enter the snapshot.

Cmn
Snapshot?

Enter the snapshot, returning the previous Snapshot for leaving this snapshot later using unsafeLeave.

Cmn
Unit

Leave the snapshot, restoring the oldSnapshot before returning.

Cmn

Inherited properties

From androidx.compose.runtime.snapshots.Snapshot
open Int

The snapshot id of the snapshot.

Cmn

Public functions

apply

open fun apply(): SnapshotApplyResult

Apply the changes made to state objects in this snapshot to the global state, or to the parent snapshot if this is a nested mutable snapshot.

Once this method returns all changes made to this snapshot are atomically visible as the global state of the state object or to the parent snapshot.

While a snapshot is active (after it is created but before apply or dispose is called) requires resources to track the values in the snapshot. Once a snapshot is no longer needed it should be either applied by calling apply or disposed by calling dispose. A snapshot that has been had is apply called can also have dispose called on it. However, calling apply after calling dispose will throw an exception.

Leaving a snapshot active could cause hard to diagnose memory leaks values are maintained by state objects for unneeded snapshots. Take care to always call dispose on any snapshot.

dispose

open fun dispose(): Unit

Dispose the snapshot. Neglecting to dispose a snapshot will result in difficult to diagnose memory leaks as it indirectly causes all state objects to maintain its value for the un-disposed snapshot.

hasPendingChanges

open fun hasPendingChanges(): Boolean

Whether there are any pending changes in this snapshot. These changes are not visible until the snapshot is applied.

takeNestedMutableSnapshot

open fun takeNestedMutableSnapshot(
    readObserver: ((Any) -> Unit)? = null,
    writeObserver: ((Any) -> Unit)? = null
): MutableSnapshot

Take a mutable snapshot of the state values in this snapshot. Entering this snapshot by calling enter allows state objects to be modified that are not visible to the this, the parent snapshot, until the apply is called.

Applying a nested snapshot, by calling apply, applies its change to, this, the parent snapshot. For a change to be visible globally, all the parent snapshots need to be applied until the root snapshot is applied to the global state.

All nested snapshots need to be disposed by calling dispose before resources associated with this snapshot can be collected. Nested active snapshots are still valid after the parent has been disposed but calling apply will fail.

takeNestedSnapshot

open fun takeNestedSnapshot(readObserver: ((Any) -> Unit)?): Snapshot

Take a snapshot of the state values in this snapshot. The resulting Snapshot is read-only. All nested snapshots need to be disposed by calling dispose before resources associated with this snapshot can be collected. Nested snapshots are still valid after the parent has been disposed.

Public properties

readOnly

open val readOnlyBoolean

True if any change to a state object in this snapshot will throw.

root

open val rootSnapshot

The root snapshot for this snapshot. For non-nested snapshots this is always this. For nested snapshot it is the parent's root.