---
title: "MutableSnapshot"
description: "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]."
type: "class"
---

<div class='type'>Class</div>


<a id='references'></a>

<div class='sourceset sourceset-common'>Common</div>


```kotlin
public open class MutableSnapshot
internal constructor(
    snapshotId: SnapshotId,
    invalid: SnapshotIdSet,
    override val readObserver: ((Any) -> Unit)?,
    override val writeObserver: ((Any) -> Unit)?,
) : Snapshot(snapshotId, invalid)
```


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


## Functions

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


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


## Companion Object



