# Snapshot

Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/snapshots/Snapshot.kt+class:androidx.compose.runtime.snapshots.Snapshot) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  sealed class Snapshot
  ```
Known direct subclasses [MutableSnapshot](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot)

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` | A snapshot of the values return by mutable states and other state objects. |

*** ** * ** ***

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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)`. The snapshot is the current snapshot as returned by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/package-summary#currentSnapshot()` until the control returns from the lambda (or until a nested `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` is called). All state objects will return the values associated with this snapshot, locally in the thread, until `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` returns. All other threads are unaffected.

Snapshots can be nested by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeNestedSnapshot(kotlin.Function1)`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateOf(kotlin.Any,androidx.compose.runtime.SnapshotMutationPolicy)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateListOf()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateMapOf()` |   |

## Summary

| ### Nested types |
|---|
| `object https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion` |

| ### Constants |
|---|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#PreexistingSnapshotId() = 1` All new state objects initial state records should be `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#PreexistingSnapshotId()` which then allows snapshots outside the creating snapshot to access the object with its initial state. | Cmn |

| ### Public companion functions |
|---|---|---|
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#global(kotlin.Function0)(block: () -> T)` Escape the current snapshot, if there is one. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#notifyObjectsInitialized()()` Notify the snapshot that all objects created in this snapshot to this point should be considered initialized. | Cmn |
| `T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)( readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, writeObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, block: () -> T )` Observe reads and or write of state objects in the current thread. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/InternalComposeApi https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#openSnapshotCount()()` | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerApplyObserver(kotlin.Function2)(observer: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Register an apply listener that is called back when snapshots are applied to the global state. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerGlobalWriteObserver(kotlin.Function1)(observer: (https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` Register an observer of the first write to the global state of a global state object since the last call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#sendApplyNotifications()`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#sendApplyNotifications()()` Send any pending apply notifications for state objects changed outside a snapshot. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)( readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?, writeObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? )` Take a snapshot of the current value of all state objects that also allows the state to be changed and later atomically applied when `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)(readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?)` Take a snapshot of the current value of all state objects. | Cmn |
| `inline R` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#withMutableSnapshot(kotlin.Function0)(block: () -> R)` Take a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` and run `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` within it. | Cmn |
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#withoutReadObservation(kotlin.Function0)(block: @https://developer.android.com/reference/kotlin/androidx/compose/runtime/DisallowComposableCalls () -> T)` Passed `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withoutReadObservation(kotlin.Function0)` will be run with all the currently set snapshot read observers disabled. | Cmn |

| ### Public companion properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#current()` Return the thread's active snapshot. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#isApplyObserverNotificationPending()` Returns whether any threads are currently in the process of notifying observers about changes to the global snapshot. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#isInSnapshot()` Return `true` if the thread is currently in the context of a snapshot. | Cmn |

| ### Protected constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#Snapshot(androidx.compose.runtime.snapshots.SnapshotId,androidx.compose.runtime.snapshots.SnapshotIdSet)(snapshotId: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotId, invalid: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotIdSet)` | Cmn |

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()()` Dispose the snapshot. | Cmn |
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)(block: () -> T)` Enter the snapshot. | Cmn |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#hasPendingChanges()()` Whether there are any pending changes in this snapshot. | Cmn |
| `abstract https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeNestedSnapshot(kotlin.Function1)(readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)?)` Take a snapshot of the state values in this snapshot. | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeEnter()()` Enter the snapshot, returning the previous `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` for leaving this snapshot later using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)(oldSnapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?)` Leave the snapshot, restoring the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)` before returning. | Cmn |

| ### Public properties |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#id()` **This property is deprecated.** Use snapshotId instead | Cmn |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#readOnly()` True if any change to a state object in this snapshot will throw. | Cmn |
| `abstract https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#root()` The root snapshot for this snapshot. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotId` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#snapshotId()` The snapshot id of the snapshot. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotContextElement` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#(androidx.compose.runtime.snapshots.Snapshot).asContextElement()()` Return a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotContextElement` that will `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` this `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` whenever the associated coroutine is resumed and leave this snapshot when it suspends. | Cmn |

## Constants

### PreexistingSnapshotId

Cmn

```
const val PreexistingSnapshotId = 1: Int
```

All new state objects initial state records should be `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#PreexistingSnapshotId()` which then allows snapshots outside the creating snapshot to access the object with its initial state.

## Public companion functions

### global

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
inline fun <T : Any?> global(block: () -> T): T
```

Escape the current snapshot, if there is one. All state objects will have the value associated with the global while the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#global(kotlin.Function0)` lambda is executing.

| Returns |
|---|---|
| `T` | the result of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#global(kotlin.Function0)` |

### notifyObjectsInitialized

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun notifyObjectsInitialized(): Unit
```

Notify the snapshot that all objects created in this snapshot to this point should be considered initialized. If any state object is are modified passed this point it will appear as modified in the snapshot and any applicable snapshot write observer will be called for the object and the object will be part of the a set of mutated objects sent to any applicable snapshot apply observer.

Unless `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#notifyObjectsInitialized()` is called, state objects created in a snapshot are not considered modified by the snapshot even if they are modified after construction.

Compose uses this between phases of composition to allow observing changes to state objects create in a previous phase.

### observe

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun <T : Any?> observe(
    readObserver: ((Any) -> Unit)? = null,
    writeObserver: ((Any) -> Unit)? = null,
    block: () -> T
): T
```

Observe reads and or write of state objects in the current thread.

This only affects the current snapshot (if any) and any new snapshots create from `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)`. It will not affect any snapshots previous created even if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` is called in `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)`.

| Parameters |
|---|---|
| `readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | called when any state object is read. |
| `writeObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | called when a state object is created or just before it is written to the first time in the snapshot or a nested mutable snapshot. This might be called several times for the same object if nested mutable snapshots are created. |
| `block: () -> T` | the code the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)` will be observing. Once `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)` returns, the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#observe(kotlin.Function1,kotlin.Function1,kotlin.Function0)` will no longer be called. |

### openSnapshotCount

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
@InternalComposeApi
fun openSnapshotCount(): Int
```

### registerApplyObserver

Cmn

```
fun registerApplyObserver(observer: (Set<Any>, Snapshot) -> Unit): ObserverHandle
```

Register an apply listener that is called back when snapshots are applied to the global state.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` to unregister `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#registerApplyObserver(kotlin.Function2)`. |

### registerGlobalWriteObserver

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun registerGlobalWriteObserver(observer: (Any) -> Unit): ObserverHandle
```

Register an observer of the first write to the global state of a global state object since the last call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#sendApplyNotifications()`.

Composition uses this to schedule a new composition whenever a state object that was read in composition is modified.

State objects can be sent to the apply observer that have not been sent to global write observers. This happens for state objects inside `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` that is later applied by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()`.

This should only be used to determine if a call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#sendApplyNotifications()` should be scheduled to be called.

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/ObserverHandle` to unregister `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#registerGlobalWriteObserver(kotlin.Function1)`. |

### sendApplyNotifications

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun sendApplyNotifications(): Unit
```

Send any pending apply notifications for state objects changed outside a snapshot.

Apply notifications for state objects modified outside snapshot are deferred until method is called. This method is implicitly called whenever a non-nested `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` is applied making its changes visible to all new, non-nested snapshots.

Composition schedules this to be called after changes to state objects are detected an observer registered with `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerGlobalWriteObserver(kotlin.Function1)`.

### takeMutableSnapshot

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun takeMutableSnapshot(
    readObserver: ((Any) -> Unit)? = null,
    writeObserver: ((Any) -> Unit)? = null
): MutableSnapshot
```

Take a snapshot of the current value of all state objects that also allows the state to be changed and later atomically applied when `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called. The values are preserved until `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` is called on the result. The global state will either see all the changes made as one atomic change, when MutableSnapshot .apply is called, or none of the changes if the mutable state object is disposed before being applied.

The values in a snapshot can be modified by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` and then, in its lambda, modify any state object. The new values of the state objects will only become visible to the global state when `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called.

An active snapshot (after it is created but before `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` is called) requires resources to track the values in the snapshot. Once a snapshot is no longer needed it should disposed by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()`.

Leaving a snapshot active could cause hard to diagnose memory leaks as values are maintained by state objects for these unneeded snapshots. Take care to always call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` on all snapshots when they are no longer needed.

A nested snapshot can be taken by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeNestedSnapshot(kotlin.Function1)`, for a read-only snapshot, or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#takeNestedMutableSnapshot(kotlin.Function1,kotlin.Function1)` for a snapshot that can be changed. Nested mutable snapshots are applied to the this, the parent snapshot, when their `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called. Their applied changes will be visible to in this snapshot but will not be visible other snapshots (including other nested snapshots) or the global state until this snapshot is applied by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()`.

Once `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called on this, the parent snapshot, all calls to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` on an active nested snapshot will fail.

Changes to a mutable snapshot are isolated, using snapshot isolation, from all other snapshots. Their changes are only visible as global state or to new snapshots once `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called.

Applying a snapshot can fail if currently visible changes to the state object conflicts with a change made in the snapshot.

When in a mutable snapshot, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)` creates a nested snapshot of the current mutable snapshot. If the current snapshot is read-only, an exception is thrown. The current snapshot is the result of calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/package-summary#currentSnapshot()` which is updated by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` which makes the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` the current snapshot while in its lambda.

Composition uses mutable snapshots to allow changes made in a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/Composable` functions to be temporarily isolated from the global state and is later applied to the global state when the composition is applied. If `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` fails applying this snapshot, the snapshot and the changes calculated during composition are disposed and a new composition is scheduled to be calculated again.

| Parameters |
|---|---|
| `readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | called when any state object is read in the lambda passed to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` or in the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` of any nested snapshots. Composition, layout and draw use `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)` to implicitly subscribe to changes to state objects to know when to update. |
| `writeObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | called when a state object is created or just before it is written to the first time in the snapshot or a nested mutable snapshot. This might be called several times for the same object if nested mutable snapshots are created. Composition uses `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)` to track when a state object is modified during composition in order to invalidate the reads that have not yet occurred. This allows a single pass of composition for state objects that are written to before they are read (such as modifying the value of a dynamic ambient provider). |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` |   |

### takeSnapshot

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
fun takeSnapshot(readObserver: ((Any) -> Unit)? = null): Snapshot
```

Take a snapshot of the current value of all state objects. The values are preserved until `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` is called on the result.

The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#takeSnapshot(kotlin.Function1)` parameter can be used to track when all state objects are read when in `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)`. A snapshot apply observer can be registered using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerApplyObserver(kotlin.Function2)` to observe modification of state objects.

An active snapshot (after it is created but before `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` is called) requires resources to track the values in the snapshot. Once a snapshot is no longer needed it should disposed by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()`.

Leaving a snapshot active could cause hard to diagnose memory leaks values as are maintained by state objects for these unneeded snapshots. Take care to always call `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` on all snapshots when they are no longer needed.

Composition uses both of these to implicitly subscribe to changes to state object and automatically update the composition when state objects read during composition change.

A nested snapshot can be taken of a snapshot which is an independent read-only copy of the snapshot and can be disposed independently. This is used by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` when in a read-only snapshot for API consistency allowing the result of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` to be disposed leaving the parent snapshot active.

| Parameters |
|---|---|
| `readObserver: ((https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)? = null` | called when any state object is read in the lambda passed to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` or in the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` of any nested snapshot. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerApplyObserver(kotlin.Function2)` |   |

### withMutableSnapshot

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
inline fun <R : Any?> withMutableSnapshot(block: () -> R): R
```

Take a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` and run `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` within it. When `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` returns successfully, attempt to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` the snapshot. Returns the result of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` or throws `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotApplyConflictException` if snapshot changes attempted by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` could not be applied.

Prior to returning, any changes made to snapshot state (e.g. state holders returned by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateOf(kotlin.Any,androidx.compose.runtime.SnapshotMutationPolicy)` are not visible to other threads. When `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#withMutableSnapshot(kotlin.Function0)` returns successfully those changes will be made visible to other threads and any snapshot observers (e.g. `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#snapshotFlow(kotlin.Function0)`) will be notified of changes.

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withMutableSnapshot(kotlin.Function0)` must not suspend if `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#withMutableSnapshot(kotlin.Function0)` is called from a suspend function.

### withoutReadObservation

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.2.0)

```
inline fun <T : Any?> withoutReadObservation(block: @DisallowComposableCalls () -> T): T
```

Passed `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot.Companion#withoutReadObservation(kotlin.Function0)` will be run with all the currently set snapshot read observers disabled.

## Public companion properties

### current

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
val current: Snapshot
```

Return the thread's active snapshot. If no thread snapshot is active then the current global snapshot is used.

### isApplyObserverNotificationPending

Cmn Added in [1.5.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.5.0)

```
val isApplyObserverNotificationPending: Boolean
```

Returns whether any threads are currently in the process of notifying observers about changes to the global snapshot.

### isInSnapshot

Cmn Added in [1.7.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.7.0)

```
val isInSnapshot: Boolean
```

Return `true` if the thread is currently in the context of a snapshot.

## Protected constructors

### Snapshot

Cmn

```
protected Snapshot(snapshotId: SnapshotId, invalid: SnapshotIdSet)
```

## Public functions

### dispose

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
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.

### enter

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
inline fun <T : Any?> enter(block: () -> T): T
```

Enter the snapshot. In `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` all state objects have the value associated with this snapshot. The value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/package-summary#currentSnapshot()` will be this snapshot until this `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` returns or a nested call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` is called. When `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` returns, the previous current snapshot is restored if there was one.

All changes to state objects inside `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` are isolated to this snapshot and are not visible to other snapshot or as global state. If this is a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#readOnly()` snapshot, any changes to state objects will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`.

For a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot`, changes made to a snapshot inside `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` can be applied atomically to the global state (or to its parent snapshot if it is a nested snapshot) by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()`.

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateOf(kotlin.Any,androidx.compose.runtime.SnapshotMutationPolicy)` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateListOf()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/package-summary#mutableStateMapOf()` |   |

### hasPendingChanges

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
abstract fun hasPendingChanges(): Boolean
```

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

### takeNestedSnapshot

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
abstract fun takeNestedSnapshot(readObserver: ((Any) -> Unit)? = null): Snapshot
```

Take a snapshot of the state values in this snapshot. The resulting `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` is read-only. All nested snapshots need to be disposed by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` before resources associated with this snapshot can be collected. Nested snapshots are still valid after the parent has been disposed.

### unsafeEnter

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.2.0)

```
fun unsafeEnter(): Snapshot?
```

Enter the snapshot, returning the previous `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` for leaving this snapshot later using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)`. Prefer `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/package-summary#(androidx.compose.runtime.snapshots.Snapshot).asContextElement()` instead of using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeEnter()` directly to prevent mismatched `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeEnter()`/`https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)` calls.

After returning all state objects have the value associated with this snapshot. The value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/package-summary#currentSnapshot()` will be this snapshot until `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)` is called with the returned `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` or another call to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeEnter()` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` is made.

All changes to state objects until another snapshot is entered or this snapshot is left are isolated to this snapshot and are not visible to other snapshot or as global state. If this is a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#readOnly()` snapshot, any changes to state objects will throw an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-state-exception/index.html`.

For a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot`, changes made to a snapshot can be applied atomically to the global state (or to its parent snapshot if it is a nested snapshot) by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()`.

### unsafeLeave

Cmn Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.2.0)

```
fun unsafeLeave(oldSnapshot: Snapshot?): Unit
```

Leave the snapshot, restoring the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeLeave(androidx.compose.runtime.snapshots.Snapshot)` before returning. See `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#unsafeEnter()`.

## Public properties

### id

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Deprecated in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.8.0)

```
open val id: Int
```

> [!CAUTION]
> **This property is deprecated.**   
> Use snapshotId instead, use this instead:
>
> ```kotlin
> snapshotId
> ```

The snapshot id of the snapshot. This is a unique number from a monotonically increasing value for each snapshot taken.

`https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#id()` will is identical to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#snapshotId()` if the value of `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#snapshotId()` is less than or equal to `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/-m-a-x_-v-a-l-u-e.html`. For `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#snapshotId()` value greater than `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/-companion/-m-a-x_-v-a-l-u-e.html`, this value will return a negative value.

### readOnly

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
abstract val readOnly: Boolean
```

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

### root

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0)

```
abstract val root: Snapshot
```

The root snapshot for this snapshot. For non-nested snapshots this is always `this`. For nested snapshot it is the parent's `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#root()`.

### snapshotId

Cmn Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.8.0)

```
open val snapshotId: SnapshotId
```

The snapshot id of the snapshot. This is a unique number from a monotonically increasing value for each snapshot taken.

## Extension functions

### Snapshot.asContextElement

Cmn Artifact: [androidx.compose.runtime:runtime](https://developer.android.com/jetpack/androidx/releases/compose-runtime) [View Source](https://cs.android.com/search?q=file:androidx/compose/runtime/snapshots/SnapshotContextElement.kt+function:asContextElement)

```
fun Snapshot.asContextElement(): SnapshotContextElement
```

Return a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotContextElement` that will `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` this `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` whenever the associated coroutine is resumed and leave this snapshot when it suspends. The snapshot still must be `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#dispose()` separately when it will no longer be used.

```kotlin
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.runtime.snapshots.asContextElement

runBlocking {
    val snapshot = Snapshot.takeSnapshot()
    try {
        withContext(snapshot.asContextElement()) {
            // Data observed by separately reading stateA and stateB are consistent with
            // the snapshot context element across suspensions
            doSomethingSuspending(someObject.stateA)
            doSomethingSuspending(someObject.stateB)
        }
    } finally {
        // Snapshot must be disposed after it will not be used again
        snapshot.dispose()
    }
}
```