# MutableSnapshot

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.MutableSnapshot) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.0.0) Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  open class MutableSnapshot : Snapshot
  ```

|---|---|---|
| [kotlin.Any](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) |||
| ↳ | [androidx.compose.runtime.snapshots.Snapshot](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot) ||
|   | ↳ | [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. 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.

All changes made in a `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` are snapshot isolated from all other snapshots and their changes can only be seen globally, or by new shots, after `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` as been called.

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

| See also |
|---|---|
| `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

| ### Public functions |
|---|---|---|
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotApplyResult` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()()` 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 https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#dispose()()` Dispose the snapshot. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#hasPendingChanges()()` Whether there are any pending changes in this snapshot. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#takeNestedMutableSnapshot(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 mutable snapshot of the state values in this snapshot. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#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 |

| ### Public properties |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#readOnly()` True if any change to a state object in this snapshot will throw. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#root()` The root snapshot for this snapshot. | Cmn |

| ### Inherited functions |
|---|
| From [androidx.compose.runtime.snapshots.Snapshot](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot) |---|---|---| | `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 | | `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 | |

| ### Inherited properties |
|---|
| From [androidx.compose.runtime.snapshots.Snapshot](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot) |---|---|---| | `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 | | `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 | |

## Public functions

### apply

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

```
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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` or disposed by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#dispose()`. A snapshot that has been had is `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` called can also have `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#dispose()` called on it. However, calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` after calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#dispose()` on any snapshot.

### dispose

Cmn

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

Cmn

```
open fun hasPendingChanges(): Boolean
```

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

### takeNestedMutableSnapshot

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

```
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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#enter(kotlin.Function0)` allows state objects to be modified that are not visible to the this, the parent snapshot, until the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` is called.

Applying a nested snapshot, by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#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 `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#dispose()` before resources associated with this snapshot can be collected. Nested active snapshots are still valid after the parent has been disposed but calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/MutableSnapshot#apply()` will fail.

### takeNestedSnapshot

Cmn

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

## Public properties

### readOnly

Cmn

```
open val readOnly: Boolean
```

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

### root

Cmn

```
open 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()`.