# PreferenceDataStoreFactory

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

*** ** * ** ***


```
public static class PreferenceDataStoreFactory
```

<br />

*** ** * ** ***

## Summary

| ### Public fields |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/PreferenceDataStoreFactory` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferenceDataStoreFactory#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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferenceDataStoreFactory#create(androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( 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 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>> 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 `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferenceDataStoreFactory#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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences> storage, 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 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>> 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 `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferenceDataStoreFactory#createWithPath(androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( 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 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>> 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://lysine.dev/okio/3.x/okio/okio/-path/index.html> produceFile )` Create an instance of `https://developer.android.com/reference/androidx/datastore/core/DataStore` using an okio Path. |

## Public fields

### INSTANCE

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

```
public @NonNull PreferenceDataStoreFactory INSTANCE
```

## Public methods

### create

```
public final @NonNull DataStore<@NonNull Preferences> create(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)
```

Create an instance of `https://developer.android.com/reference/androidx/datastore/core/DataStore`. 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.

| Parameters |
|---|---|
| `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 cannot 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>> migrations` | 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. |
| `@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 PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. File will be created if it doesn't exist. |

| 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |

### create

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

Create an instance of `https://developer.android.com/reference/androidx/datastore/core/DataStore`. 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.

| 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences> storage` | The storage object defines where and how the preferences will be stored. |
| `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 cannot 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>> migrations` | 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. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. |

| 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |

### createWithPath

```
public final @NonNull DataStore<@NonNull Preferences> createWithPath(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull Path> produceFile
)
```

Create an instance of `https://developer.android.com/reference/androidx/datastore/core/DataStore` using an okio Path. 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.

| Parameters |
|---|---|
| `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 cannot 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>> migrations` | 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. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull https://lysine.dev/okio/3.x/okio/okio/-path/index.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 PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. File will be created if it doesn't exist. |

| 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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |