# StateStoreKt

Added in 1.0.0-alpha02

*** ** * ** ***


```
public final class StateStoreKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final void` | `https://developer.android.com/reference/androidx/appstate/datastore/StateStoreKt#(androidx.appstate.statestore.StateStore).syncToDataStore(androidx.datastore.core.DataStore)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStore receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/datastore/StateStorePreferences> dataStore )` Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and syncs keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `static final void` | `https://developer.android.com/reference/androidx/appstate/datastore/StateStoreKt#(androidx.appstate.statestore.StateStore).syncToDataStore(kotlin.String,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/statestore/StateStore receiver, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html path, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html coroutineScope )` Tracks changes to `https://developer.android.com/reference/androidx/appstate/statestore/StateStore` and persists keys implementing `https://developer.android.com/reference/androidx/appstate/datastore/PersistToDataStore` to a `https://developer.android.com/reference/androidx/datastore/core/DataStore` backed by a file. |

## Public methods

### 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)

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

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

Keys must implement `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/appstate/datastore/StateStorePreferences> dataStore` | the `https://developer.android.com/reference/androidx/datastore/core/DataStore` used to save and restore state |

### 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)

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

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

Keys must implement `https://developer.android.com/reference/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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html path` | the file path to save and restore state from |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html coroutineScope` | the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html` used for the `https://developer.android.com/reference/androidx/datastore/core/DataStore` |