StateStoreKt

Added in 1.0.0-alpha01

public final class StateStoreKt


Summary

Public methods

static final void

Tracks changes to StateStore and syncs keys implementing PersistToDataStore to DataStore.

static final void
syncToDataStore(
    @NonNull StateStore receiver,
    @NonNull String path,
    @NonNull CoroutineScope coroutineScope
)

Tracks changes to StateStore and persists keys implementing PersistToDataStore to a DataStore backed by a file.

Public methods

public static final void syncToDataStore(
    @NonNull StateStore receiver,
    @NonNull DataStore<@NonNull StateStorePreferences> dataStore
)

Tracks changes to StateStore and syncs keys implementing PersistToDataStore to DataStore.

Keys must implement PersistToDataStore to be saved.

import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

stateStore.syncToDataStore(dataStore)
Parameters
@NonNull DataStore<@NonNull StateStorePreferences> dataStore

the DataStore used to save and restore state

public static final void syncToDataStore(
    @NonNull StateStore receiver,
    @NonNull String path,
    @NonNull CoroutineScope coroutineScope
)

Tracks changes to StateStore and persists keys implementing PersistToDataStore to a DataStore backed by a file.

Keys must implement PersistToDataStore to be saved.

import androidx.appstate.datastore.syncToDataStore
import androidx.appstate.statestore.StateStore
import androidx.datastore.core.DataStore

val stateStore = StateStore()

// Register the key by getting the state
val state = stateStore.getState(SampleKey)

// The path must include the directory, for example, on Android:
// val path = context.filesDir.resolve("stateStore.preferences_pb").absolutePath
val path = "/path/to/directory/stateStore.preferences_pb"

stateStore.syncToDataStore(path, CoroutineScope(Dispatchers.IO))
Parameters
@NonNull String path

the file path to save and restore state from

@NonNull CoroutineScope coroutineScope

the CoroutineScope used for the DataStore