# SnapshotObserver

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/tooling/SnapshotObserver.kt+class:androidx.compose.runtime.snapshots.tooling.SnapshotObserver) Added in [1.8.0](https://developer.android.com/jetpack/androidx/releases/compose-runtime#1.8.0) Common/All
- Cmn

  ```
  @ExperimentalComposeRuntimeApi
  interface SnapshotObserver
  ```

*** ** * ** ***

An observer for the snapshot system that notifies an observer when a snapshot is created, applied, and/or disposed.

All methods are called in the thread of the snapshot so all observers must be thread safe as they may be called from any thread.

Calling any of the Snapshot API (including, reading or writing mutable state objects) is not supported and may produce inconsistent result or throw an exception.

## Summary

| ### Public functions |
|---|---|---|
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onApplied(androidx.compose.runtime.snapshots.Snapshot,kotlin.collections.Set)(snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot, changed: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>)` Called after a snapshot is applied. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onCreated(androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.tooling.SnapshotInstanceObservers)( snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot, parent: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?, observers: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotInstanceObservers? )` Called after snapshot is created. | Cmn |
| `open https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotInstanceObservers?` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onPreCreate(androidx.compose.runtime.snapshots.Snapshot,kotlin.Boolean)(parent: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?, readonly: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Called before a snapshot is created allowing reads and writes to the snapshot to be observed. | Cmn |
| `open https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/annotation/EmptySuper https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onPreDispose(androidx.compose.runtime.snapshots.Snapshot)(snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot)` Called while a snapshot is being disposed. | Cmn |

## Public functions

### onApplied

Cmn

```
@EmptySuper
open fun onApplied(snapshot: Snapshot, changed: Set<Any>): Unit
```

Called after a snapshot is applied.

For nested snapshots, the changes will only be visible to the parent snapshot, not globally. Snapshots do not have a parent will have changes that are visible globally and such notification are equivalent the notification sent to `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#registerApplyObserver(kotlin.Function2)` and will include all objects modified by any nested snapshots that have been applied to the parent snapshot.

This method is called in the same thread that applies the snapshot.

| Parameters |
|---|---|
| `snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | the snapshot that was applied. |
| `changed: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>` | the set of objects that were modified during the snapshot. |

### onCreated

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

```
@EmptySuper
open fun onCreated(
    snapshot: Snapshot,
    parent: Snapshot?,
    observers: SnapshotInstanceObservers?
): Unit
```

Called after snapshot is created.

This is called prior to the instance being returned by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeSnapshot(kotlin.Function1)` or `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#takeMutableSnapshot(kotlin.Function1,kotlin.Function1)`.

This method is called in the same thread that creates the snapshot.

| Parameters |
|---|---|
| `snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | the snapshot that was created. |
| `parent: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?` | the parent snapshot for the new snapshot if it is a nested snapshot or null if it is a root snapshot. |
| `observers: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotInstanceObservers?` | the read and write observers that were installed by the value returned by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onCreated(androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.tooling.SnapshotInstanceObservers)`. This allows correlating which snapshot observers returned by `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onPreCreate(androidx.compose.runtime.snapshots.Snapshot,kotlin.Boolean)` to the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotObserver#onCreated(androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.Snapshot,androidx.compose.runtime.snapshots.tooling.SnapshotInstanceObservers)` that was created. |

### onPreCreate

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

```
open fun onPreCreate(parent: Snapshot?, readonly: Boolean): SnapshotInstanceObservers?
```

Called before a snapshot is created allowing reads and writes to the snapshot to be observed.

This method is called in the same thread that creates the snapshot.

| Parameters |
|---|---|
| `parent: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot?` | the parent snapshot for the new snapshot if it is a nested snapshot or null otherwise. |
| `readonly: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | whether the snapshot being created will be read-only. |

| Returns |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/tooling/SnapshotInstanceObservers?` | optional read and write observers that will be added to the snapshot created. |

### onPreDispose

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

```
@EmptySuper
open fun onPreDispose(snapshot: Snapshot): Unit
```

Called while a snapshot is being disposed.

This method is called in the same thread that disposes the snapshot.

| Parameters |
|---|---|
| `snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot` | information about the snapshot that was created. |