# StateStore

Artifact: [androidx.appstate.statestore:statestore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/statestore/StateStore.kt+class:androidx.appstate.statestore.StateStore) Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

*** ** * ** ***

Common/All
- Cmn

  ```
  class StateStore
  ```

*** ** * ** ***

Class that contains a store that maintains compose state beyond individual Android components.

There is no limitation on where these instance should be instantiated as that is left up to the developer.

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#StateStore()()` | Cmn |

| ### Public functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/runtime/State<T>` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#getState(androidx.appstate.statestore.StateStoreKey,kotlin.Any)(stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>, defaultValue: T)` Returns the state associated with the given key. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#setState(androidx.appstate.statestore.StateStoreKey,kotlin.Any)(stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>, value: T)` Sets the state for the given key. | Cmn |
| `inline https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#updateState(androidx.appstate.statestore.StateStoreKey,kotlin.Any,kotlin.Function1)( stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>, defaultValue: T, update: (T) -> T )` Retrieves the current state and uses that with the update lambda to set a new state for the specified key. | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-set/index.html<https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<*>>` | `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#keys()` The set of keys that are currently added to this StateStore. | Cmn |

| ### Extension functions |
|---|---|---|
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore.https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#(androidx.appstate.statestore.StateStore).syncToDataStore(androidx.datastore.core.DataStore)( dataStore: https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStorePreferences> )` Tracks changes to `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore` and syncs keys implementing `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`. | android |
| `suspend https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore.https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore#(androidx.appstate.statestore.StateStore).syncToDataStore(kotlin.String,kotlinx.coroutines.CoroutineScope)(path: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, coroutineScope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html)` Tracks changes to `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore` and persists keys implementing `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to a `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` backed by a file. | android |

## Public constructors

### StateStore

Cmn Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
StateStore()
```

## Public functions

### getState

Cmn Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
fun <T : Any?> getState(
    stateKey: StateStoreKey<T>,
    defaultValue: T = stateKey.defaultValue
): State<T>
```

Returns the state associated with the given key.

| Parameters |
|---|---|
| `stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>` | the key of the state to retrieve |
| `defaultValue: T = stateKey.defaultValue` | the value to put if there is no value for the key |

### setState

Cmn Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
fun <T : Any?> setState(stateKey: StateStoreKey<T>, value: T): Unit
```

Sets the state for the given key.

If this is the first time the state is being set a listener is added to allow the given key to be automatically cleared from the map based on the `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey#autoClearKey()` and `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey#shouldClearState()`. For the automatic clearing, `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey#autoClearKey()` must be non-null, the value associated with this key must be non-null, and the `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey#shouldClearState()` condition must return true. AS long as all 3 of those conditions are not met, the listener will not be removed.

| Parameters |
|---|---|
| `stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>` | the key to set a value for |
| `value: T` | the value to set inside the StateStore |

### updateState

Cmn Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
inline fun <T : Any?> updateState(
    stateKey: StateStoreKey<T>,
    defaultValue: T = stateKey.defaultValue,
    update: (T) -> T
): Unit
```

Retrieves the current state and uses that with the update lambda to set a new state for the specified key.

| Parameters |
|---|---|
| `stateKey: https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey<T>` | the key to update |
| `defaultValue: T = stateKey.defaultValue` | the value to use if no state has been set for the key |
| `update: (T) -> T` | lambda used to update the state using the current state |

## Public properties

### keys

Cmn Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
val keys: Set<StateStoreKey<*>>
```

The set of keys that are currently added to this StateStore.

This can be used in conjunction with the androidx.stateStore.transform APIs to provide a mechanism for reacting to state changes.

```kotlin
val scope = CoroutineScope(context + SupervisorJob())
val job = scope.launch {
    listen {
        val keys = stateStore.keys
        // Do some processing of the keys
    }
}
```

## Extension functions

### StateStore.syncToDataStore

android Artifact: [androidx.appstate.statestore:statestore-datastore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/datastore/StateStore.kt+function:syncToDataStore)

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

Tracks changes to `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore` and syncs keys implementing `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`.

Keys must implement `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to be saved.

```kotlin
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: https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStorePreferences>` | the `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` used to save and restore state |

### StateStore.syncToDataStore

android Artifact: [androidx.appstate.statestore:statestore-datastore](https://developer.android.com/jetpack/androidx/releases/appstate-statestore) [View Source](https://cs.android.com/search?q=file:androidx/appstate/datastore/StateStore.kt+function:syncToDataStore)

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

Tracks changes to `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore` and persists keys implementing `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to a `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` backed by a file.

Keys must implement `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` to be saved.

```kotlin
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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | the file path to save and restore state from |
| `coroutineScope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` | the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` used for the `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` |