# DataStoreDelegateKt

Added in 1.0.0

*** ** * ** ***


```
public final class DataStoreDelegateKt
```

<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 T>>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/DataStoreDelegateKt#dataStore(kotlin.String,androidx.datastore.core.Serializer,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 fileName, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> serializer, https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> 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 T>>> 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. |
| `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 T>>` | `@https://developer.android.com/reference/androidx/annotation/RequiresApi(value = 24) <T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/DataStoreDelegateKt#deviceProtectedDataStore(kotlin.String,androidx.datastore.core.Serializer,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 fileName, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> serializer, https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> 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 T>>> 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 to be used in direct boot. |

## Public methods

### dataStore

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

```
public static final @NonNull ReadOnlyProperty<@NonNull Context, @NonNull DataStore<@NonNull T>> <T extends Object> dataStore(
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull Function1<@NonNull Context, @NonNull List<@NonNull DataMigration<@NonNull T>>> 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 dataStore("filename", serializer)

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

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html fileName` | the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from `https://developer.android.com/reference/androidx/datastore/package-summary#(android.content.Context).dataStoreFile(kotlin.String)`. It is created in the "/datastore" subdirectory, in the user encrypted storage. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> serializer` | The serializer for `T`. |
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> 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 T>>> 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 T>>` | a property delegate that manages a datastore as a singleton. |

### deviceProtectedDataStore

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

```
@RequiresApi(value = 24)
public static final @NonNull ReadOnlyProperty<@NonNull Context, @NonNull DataStore<@NonNull T>> <T extends Object> deviceProtectedDataStore(
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull Function1<@NonNull Context, @NonNull List<@NonNull DataMigration<@NonNull T>>> produceMigrations,
    @NonNull CoroutineScope scope
)
```

Creates a property delegate for a single process DataStore to be used in direct boot.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html fileName` | the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from `https://developer.android.com/reference/androidx/datastore/package-summary#(android.content.Context).dataStoreFile(kotlin.String)`. It is created in the "/datastore" subdirectory, in the user encrypted storage. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Serializer<@https://developer.android.com/reference/androidx/annotation/NonNull T> serializer` | The serializer for `T`. |
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> 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 T>>> 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 T>>` | a property delegate that manages a datastore as a singleton. |