Google is committed to advancing racial equity for Black communities. See how.

SnapshotStateObserver

class SnapshotStateObserver
kotlin.Any
   ↳ androidx.compose.runtime.snapshots.SnapshotStateObserver

Summary

Public constructors
<init>(onChangedExecutor: (callback: () -> Unit) -> Unit)

Public methods
Unit
clear(target: Any)

Clears all model read observations for a given target.

Unit

Remove all hooks used to track changes.

Unit

Starts or stops watching for model commits based on enabled.

Unit
observeReads(target: T, onChanged: (T) -> Unit, block: () -> Unit)

Executes block, observing state object reads during its execution.

Unit
pauseObservingReads(block: () -> Unit)

Stops observing state object reads while executing block.

Public constructors

<init>

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

Public methods

clear

fun clear(target: Any): Unit

Clears all model read observations for a given target. This clears values for all onCommit methods passed in observeReads.

dispose

fun dispose(): Unit

Remove all hooks used to track changes.

enableStateUpdatesObserving

fun enableStateUpdatesObserving(enabled: Boolean): Unit

Starts or stops watching for model commits based on enabled.

observeReads

fun <T : Any> observeReads(
    target: T,
    onChanged: (T) -> Unit,
    block: () -> Unit
): Unit

Executes block, observing state object reads during its execution.

The target is stored as a weak reference to be passed to onChanged when a change to the state object has been detected.

Observation for target will be paused when a new observeReads call is made or when pauseObservingReads is called.

Any previous observation with the given target and onChanged will be cleared and only the new observation on block will be stored. It is important that the same instance of onChanged is used between calls or previous references will not be cleared.

The onChanged will be called when a state object that was accessed during block has been applied, and it will be called with onChangedExecutor.

pauseObservingReads

fun pauseObservingReads(block: () -> Unit): Unit

Stops observing state object reads while executing block. State object reads may be restarted by calling observeReads inside block.