# DataStoreFactory

Artifact: [androidx.datastore:datastore-core](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/core/DataStoreFactory.+class:androidx.datastore.core.DataStoreFactory) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

*** ** * ** ***


```
public static class DataStoreFactory
```

<br />

*** ** * ** ***

Public factory for creating DataStore instances.

## Summary

| ### Public fields |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory` | `https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory#INSTANCE()` |

| ### Public methods |
|---|---|
| `final @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/core/DataStoreFactory#create(androidx.datastore.core.Storage,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Storage<@https://developer.android.com/reference/androidx/annotation/NonNull T> storage, 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 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>> migrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope )` |
| `final @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/core/DataStoreFactory#create(androidx.datastore.core.Serializer,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( @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 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>> migrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/File.html> produceFile )` Create an instance of SingleProcessDataStore. |
| `final @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/core/DataStoreFactory#create(androidx.datastore.core.Serializer,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( @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 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>> migrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope, @https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/File.html> produceFile )` Create an instance of SingleProcessDataStore. |
| `final @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/core/DataStoreFactory#createInDeviceProtectedStorage(android.content.Context,kotlin.String,androidx.datastore.core.Serializer,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context, @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 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>> migrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope )` Create an instance of SingleProcessDataStore that can be used during direct boot. |
| `final @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/core/DataStoreFactory#createWithTracing(androidx.datastore.core.Storage,androidx.tracing.Tracer,androidx.datastore.core.CorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Storage<@https://developer.android.com/reference/androidx/annotation/NonNull T> storage, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/tracing/Tracer tracer, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/CorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> corruptionHandler, @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>> migrations, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html scope )` Create an instance of SingleProcessDataStore with tracing enabled. |

## Public fields

### INSTANCE

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.0.0)

```
public @NonNull DataStoreFactory INSTANCE
```

## Public methods

### create

```
public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Storage<@NonNull T> storage,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope
)
```

### create

```
public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)
```

Create an instance of SingleProcessDataStore. Never create more than one instance of DataStore for a given file; doing so can break all DataStore functionality. You should consider managing your DataStore instance as a singleton. If there are multiple DataStores active, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active.

T is the type DataStore acts on. The type T must be immutable. Mutating a type used in DataStore invalidates any guarantees that DataStore provides and will result in potentially serious, hard-to-catch bugs. We strongly recommend using protocol buffers: https://developers.google.com/protocol-buffers/docs/javatutorial - which provides immutability guarantees, a simple API and efficient serialization.

It is important to note that if a `https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory#create(androidx.datastore.core.Serializer,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)` lambda to produce a file in the User Encrypted (UE) storage via a `https://developer.android.com/reference/android/content/Context.html` is used, this file will not be available during direct boot, and may result in a `https://developer.android.com/reference/androidx/datastore/core/DirectBootUsageException` via a `https://developer.android.com/reference/java/io/FileNotFoundException.html` or a silent failure. To create an instance of a DataStore to be used safely during direct boot mode, please use `https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory#createInDeviceProtectedStorage(android.content.Context,kotlin.String,androidx.datastore.core.Serializer,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)`.

| Parameters |
|---|---|
| `@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` | Serializer for the type T used with DataStore. The type T must be immutable. |
| `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 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>> migrations` | Migrations are run before any access to data can occur. Migrations must be idempotent. |
| `@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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/File.html> produceFile` | Function which returns the file that the new DataStore will act on. The function must return the same path every time. No two instances of DataStore should act on the same file at the same time. |

| Returns |
|---|---|
| `@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 new DataStore instance with the provided configuration |

### create

```
public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)
```

Create an instance of SingleProcessDataStore. Never create more than one instance of DataStore for a given file; doing so can break all DataStore functionality. You should consider managing your DataStore instance as a singleton. If there are multiple DataStores active, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active.

T is the type DataStore acts on. The type T must be immutable. Mutating a type used in DataStore invalidates any guarantees that DataStore provides and will result in potentially serious, hard-to-catch bugs. We strongly recommend using protocol buffers: https://developers.google.com/protocol-buffers/docs/javatutorial - which provides immutability guarantees, a simple API and efficient serialization.

| Parameters |
|---|---|
| `@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` | Serializer for the type T used with DataStore. The type T must be immutable. |
| `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 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>> migrations` | Migrations are run before any access to data can occur. Migrations must be idempotent. |
| `@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. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/File.html> produceFile` | Function which returns the file that the new DataStore will act on. The function must return the same path every time. No two instances of DataStore should act on the same file at the same time. |

| Returns |
|---|---|
| `@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 new DataStore instance with the provided configuration |

### createInDeviceProtectedStorage

```
@RequiresApi(value = 24)
public final @NonNull DataStore<@NonNull T> <T extends Object> createInDeviceProtectedStorage(
    @NonNull Context context,
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope
)
```

Create an instance of SingleProcessDataStore that can be used during direct boot.

This API always creates the DataStore in the Device Encrypted storage.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | The DeviceProtectedStorageContext which will be used to create the file that the new DataStore will act on. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html fileName` | the filename relative to Context.createDeviceProtectedStorageContext().filesDir that DataStore acts on. The File is obtained from `https://developer.android.com/reference/androidx/datastore/core/package-summary#(android.content.Context).deviceProtectedDataStoreFile(kotlin.String)`. It is created in the "/datastore" subdirectory, in the device 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` | Serializer for the type T used with DataStore. The type T must be immutable. |
| `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 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>> migrations` | Migrations are run before any access to data can occur. Migrations must be idempotent. |
| `@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://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | a new DataStore instance with the provided configuration |

### createWithTracing

```
public final @NonNull DataStore<@NonNull T> <T extends Object> createWithTracing(
    @NonNull Storage<@NonNull T> storage,
    @NonNull Tracer tracer,
    @NonNull CorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope
)
```

Create an instance of SingleProcessDataStore with tracing enabled.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/Storage<@https://developer.android.com/reference/androidx/annotation/NonNull T> storage` | Storage for the type T used with DataStore. The type T must be immutable. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/tracing/Tracer tracer` | The `https://developer.android.com/reference/androidx/tracing/Tracer` used to instrument and trace DataStore operations. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/CorruptionHandler<@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 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>> migrations` | Migrations are run before any access to data can occur. Migrations must be idempotent. |
| `@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://developer.android.com/reference/androidx/datastore/core/DataStore<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | a new DataStore instance with the provided configuration. |