BackgroundThreadStateHandler


@UnstableApi
public final class BackgroundThreadStateHandler<T extends Object>


Helper class to handle state updates on a background thread while maintaining a placeholder state on the foreground thread.

Parameters
<T extends Object>

An immutable object representing the entire state. Must implement equals.

Summary

Nested types

An interface to handle changes to the state on the foreground thread.

Public constructors

BackgroundThreadStateHandler(
    T initialState,
    Looper backgroundLooper,
    Looper foregroundLooper,
    Clock clock,
    BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged
)

Creates the helper for background thread state updates.

Public methods

T
get()

Returns the current state.

void

Runs the provided Runnable on the background thread.

void

Updates the background state directly, independent to any operation started from the foreground thread.

void
updateStateAsync(
    Function<T, T> placeholderState,
    Function<T, T> backgroundStateUpdate
)

Starts an asynchronous state update.

Public constructors

BackgroundThreadStateHandler

public BackgroundThreadStateHandler(
    T initialState,
    Looper backgroundLooper,
    Looper foregroundLooper,
    Clock clock,
    BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged
)

Creates the helper for background thread state updates.

This constructor may be called on any thread.

Parameters
T initialState

The initial state value.

Looper backgroundLooper

The Looper to run background operations on.

Looper foregroundLooper

The Looper to run foreground operations on.

Clock clock

The Clock to control the handler messages.

BackgroundThreadStateHandler.StateChangeListener<T> onStateChanged

The StateChangeListener to listen to state changes.

Public methods

get

public T get()

Returns the current state.

Can be called on either the foreground or background thread, returning the respective current state of this thread.

runInBackground

public void runInBackground(Runnable runnable)

Runs the provided Runnable on the background thread.

Can be called from any thread.

Note: This method is useful to update the state on the background using setStateInBackground for events arriving from external sources. Use updateStateAsync if the intention is update the state in response the a foreground thread method call.

Parameters
Runnable runnable

The Runnable to be called on the background thread.

setStateInBackground

public void setStateInBackground(T newState)

Updates the background state directly, independent to any operation started from the foreground thread.

Must only be called on the background thread.

Parameters
T newState

The new state.

updateStateAsync

public void updateStateAsync(
    Function<T, T> placeholderState,
    Function<T, T> backgroundStateUpdate
)

Starts an asynchronous state update.

Must only be called on the foreground thread.

Parameters
Function<T, T> placeholderState

A function to create a placeholder state from the current state while the operation is pending. Will be called on the foreground thread.

Function<T, T> backgroundStateUpdate

A function to handle the background state update, taking in the current background state and returning the updated state. Will be called on the background thread.