# SnapshotStateObserver

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

  ```
  class SnapshotStateObserver
  ```

*** ** * ** ***

Helper class to efficiently observe snapshot state reads. See `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` for more details.

NOTE: This class is not thread-safe, so implementations should not reuse observer between different threads to avoid race conditions.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#SnapshotStateObserver(kotlin.Function1)(onChangedExecutor: (callback: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` | Cmn |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clear()()` Remove all observations. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clear(kotlin.Any)(scope: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html)` Clears all state read observations for a given `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clear(kotlin.Any)`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clearIf(kotlin.Function1)(predicate: (scope: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Remove observations using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clearIf(kotlin.Function1)` to identify scopes to be removed. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `@https://developer.android.com/reference/kotlin/androidx/compose/runtime/TestOnly https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#notifyChanges(kotlin.collections.Set,androidx.compose.runtime.snapshots.Snapshot)(changes: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html>, snapshot: https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot)` This method is only used for testing. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html> https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)( scope: T, onValueChangedForScope: (T) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html, block: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html )` Executes `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)`, observing state object reads during its execution. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#start()()` Starts watching for state commits. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#stop()()` Stops watching for state commits. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `[withNoObservations](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#withNoObservations(kotlin.Function0))(block: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)` **This function is deprecated.** Replace with Snapshot.withoutReadObservation() | Cmn |

## Public constructors

### SnapshotStateObserver

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

```
SnapshotStateObserver(onChangedExecutor: (callback: () -> Unit) -> Unit)
```

## Public functions

### clear

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

```
fun clear(): Unit
```

Remove all observations.

### clear

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

```
fun clear(scope: Any): Unit
```

Clears all state read observations for a given `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clear(kotlin.Any)`. This clears values for all `onValueChangedForScope` callbacks passed in `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)`.

### clearIf

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

```
fun clearIf(predicate: (scope: Any) -> Boolean): Unit
```

Remove observations using `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#clearIf(kotlin.Function1)` to identify scopes to be removed. This is used when a scope is no longer in the hierarchy and should not receive any callbacks.

### notifyChanges

Cmn

```
@TestOnly
fun notifyChanges(changes: Set<Any>, snapshot: Snapshot): Unit
```

This method is only used for testing. It notifies that `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#notifyChanges(kotlin.collections.Set,androidx.compose.runtime.snapshots.Snapshot)` have been made on `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#notifyChanges(kotlin.collections.Set,androidx.compose.runtime.snapshots.Snapshot)`.

### observeReads

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

```
fun <T : Any> observeReads(
    scope: T,
    onValueChangedForScope: (T) -> Unit,
    block: () -> Unit
): Unit
```

Executes `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)`, observing state object reads during its execution.

The `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` and `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` are associated with any values that are read so that when those values change, `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` will be called with the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` parameter.

Observation can be paused with `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/Snapshot#withoutReadObservation(kotlin.Function0)`.

| Parameters |
|---|---|
| `scope: T` | value associated with the observed scope. |
| `onValueChangedForScope: (T) -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | is called with the `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` when value read within `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` has been changed. For repeated observations, it is more performant to pass the same instance of the callback, as `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observedScopeMaps()` grows with each new callback instance. |
| `block: () -> https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | to observe reads within. |

### start

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

```
fun start(): Unit
```

Starts watching for state commits.

### stop

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

```
fun stop(): Unit
```

Stops watching for state commits.

### withNoObservations

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

```
fun [withNoObservations](https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#withNoObservations(kotlin.Function0))(block: () -> Unit): Unit
```

> [!CAUTION]
> **This function is deprecated.**   
> Replace with Snapshot.withoutReadObservation(), use this instead:
>
> ```kotlin
> Snapshot.withoutReadObservation(block)
> ```

Stops observing state object reads while executing `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#withNoObservations(kotlin.Function0)`. State object reads may be restarted by calling `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#observeReads(kotlin.Any,kotlin.Function1,kotlin.Function0)` inside `https://developer.android.com/reference/kotlin/androidx/compose/runtime/snapshots/SnapshotStateObserver#withNoObservations(kotlin.Function0)`.