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

ModelObserver

class ModelObserver
kotlin.Any
   ↳ androidx.ui.core.ModelObserver

Allows for easy model read observation. To begin observe a change, you must pass a non-lambda onCommit listener to the observeReads method.

When a state change has been committed, the onCommit listener will be called with the targetObject as the argument. There are no order guarantees for onCommit listener calls. Commit callbacks are made on the thread that model changes are committed, so the commitExecutor allows the developer to control the thread on which the onCommitcalls are made. An example use would be to have the executor shift to the the UI thread for the onCommit callbacks to be made.

A different ModelObserver should be used with each thread that observeReads is called on.

Summary

Public constructors
<init>(commitExecutor: (command: () -> Unit) -> Unit)

Allows for easy model read observation.

Public methods
Unit
clear(target: Any)

Clears all model read observations for a given target.

Unit

Starts or stops watching for model commits based on enabled.

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

Executes block, observing model reads during its execution.

Unit
pauseObservingReads(block: () -> Unit)

Stops observing model reads while executing block.

Public constructors

<init>

ModelObserver(commitExecutor: (command: () -> Unit) -> Unit)

Allows for easy model read observation. To begin observe a change, you must pass a non-lambda onCommit listener to the observeReads method.

When a state change has been committed, the onCommit listener will be called with the targetObject as the argument. There are no order guarantees for onCommit listener calls. Commit callbacks are made on the thread that model changes are committed, so the commitExecutor allows the developer to control the thread on which the onCommitcalls are made. An example use would be to have the executor shift to the the UI thread for the onCommit callbacks to be made.

A different ModelObserver should be used with each thread that observeReads is called on.

Parameters
commitExecutor: (command: () -> Unit) -> Unit The executor on which all onCommit calls will be made.

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.

enableModelUpdatesObserving

fun enableModelUpdatesObserving(enabled: Boolean): Unit

Starts or stops watching for model commits based on enabled.

observeReads

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

Executes block, observing model reads during its execution. The target is stored as a weak reference to be passed to onCommit when a change to the model 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 onCommit will be cleared and only the new observation on block will be stored. It is important that the same instance of onCommit is used between calls or previous references will not be cleared.

The onCommit will be called when a model that was accessed during block has been committed, and it will be called with commitExecutor.

pauseObservingReads

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

Stops observing model reads while executing block. Model reads may be restarted by calling observeReads inside block.