# PreferenceDataStoreDelegateKt

Added in 1.0.0

*** ** * ** ***


```
public final class PreferenceDataStoreDelegateKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-only-property/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @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/datastore/preferences/core/Preferences>>` | `https://developer.android.com/reference/androidx/datastore/preferences/PreferenceDataStoreDelegateKt#preferencesDataStore(kotlin.String,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.Function1,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html name, https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences> corruptionHandler, @https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataMigration<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>>> produceMigrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope )` Creates a property delegate for a single process DataStore. |

## Public methods

### preferencesDataStore

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

```
public static final @NonNull ReadOnlyProperty<@NonNull Context, @NonNull DataStore<@NonNull Preferences>> preferencesDataStore(
    @NonNull String name,
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull Function1<@NonNull Context, @NonNull List<@NonNull DataMigration<@NonNull Preferences>>> produceMigrations,
    @NonNull CoroutineScope scope
)
```

Creates a property delegate for a single process DataStore. This should only be called once in a file (at the top level), and all usages of the DataStore should use a reference the same Instance. The receiver type for the property delegate must be an instance of `https://developer.android.com/reference/android/content/Context.html`.

This should only be used from a single application in a single classloader in a single process.

Example usage:

```
val Context.myDataStore by preferencesDataStore("filename")

class SomeClass(val context: Context) {
   suspend fun update() = context.myDataStore.edit {...}
}
```

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html name` | The name of the preferences. The preferences will be stored in a file in the "datastore/" subdirectory in the application context's files directory and is generated using `https://developer.android.com/reference/androidx/datastore/preferences/package-summary#(android.content.Context).preferencesDataStoreFile(kotlin.String)`. |
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences> corruptionHandler` | The corruptionHandler is invoked if DataStore encounters a `https://developer.android.com/reference/androidx/datastore/core/CorruptionException` when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/List.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataMigration<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>>> produceMigrations` | produce the migrations. The ApplicationContext is passed in to these callbacks as a parameter. DataMigrations are run before any access to data can occur. Each producer and migration may be run more than once whether or not it already succeeded (potentially because another migration failed or a write to disk failed.) |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope` | The scope in which IO operations and transform functions will execute. |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.properties/-read-only-property/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html, @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/datastore/preferences/core/Preferences>>` | a property delegate that manages a datastore as a singleton. |