androidx.appstate.datastore


Interfaces

PersistToDataStore

Marks a StateStoreKey to be persisted to DataStore.

Classes

StateStorePreferences

Preferences map for StateStore backed by DataStore.

Objects

StateStoreSerializer

Serializer for StateStorePreferences using DataStore and Okio.

Extension functions summary

suspend Unit

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

suspend Unit
StateStore.syncToDataStore(path: String, coroutineScope: CoroutineScope)

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

Extension functions

StateStore.syncToDataStore

suspend fun StateStore.syncToDataStore(
    dataStore: DataStore<StateStorePreferences>
): Unit

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
dataStore: DataStore<StateStorePreferences>

the DataStore used to save and restore state

StateStore.syncToDataStore

suspend fun StateStore.syncToDataStore(path: String, coroutineScope: CoroutineScope): Unit

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
path: String

the file path to save and restore state from

coroutineScope: CoroutineScope

the CoroutineScope used for the DataStore