# androidx.appstate.datastore

*** ** * ** ***

## Interfaces

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/PersistToDataStore` | Marks a `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStoreKey` to be persisted to `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`. |

## Classes

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStorePreferences` | Preferences map for `https://developer.android.com/reference/kotlin/androidx/appstate/statestore/StateStore` backed by `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`. |

## Objects

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStoreSerializer` | Serializer for `https://developer.android.com/reference/kotlin/androidx/appstate/datastore/StateStorePreferences` using `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` and Okio. |

## Extension functions summary

|---|---|
| `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/datastore/package-summary#(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`. |
| `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/datastore/package-summary#(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. |

## Extension functions

### StateStore.syncToDataStore

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) Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
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

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) Added in [1.0.0-alpha02](https://developer.android.com/jetpack/androidx/releases/appstate-statestore#1.0.0-alpha02)

```
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` |