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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  object PreferenceDataStoreFactory
  ```

*** ** * ** ***

## Summary

| ### Public functions |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferenceDataStoreFactory#create(androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>?, migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>>, scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html, produceFile: () -> https://developer.android.com/reference/java/io/File.html )` Create an instance of `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`. | android |
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferenceDataStoreFactory#create(androidx.datastore.core.Storage,androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope)( storage: https://developer.android.com/reference/kotlin/androidx/datastore/core/Storage<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>, corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>?, migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>>, scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html )` Create an instance of `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore`. | Cmn android N JS |
| `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | `https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/PreferenceDataStoreFactory#createWithPath(androidx.datastore.core.handlers.ReplaceFileCorruptionHandler,kotlin.collections.List,kotlinx.coroutines.CoroutineScope,kotlin.Function0)( corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>?, migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>>, scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html, produceFile: () -> https://lysine.dev/okio/3.x/okio/okio/-path/index.html )` Create an instance of `https://developer.android.com/reference/kotlin/androidx/datastore/core/DataStore` using an okio Path. | Cmn android N JS |

## Public functions

### create

android

```
fun create(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
    produceFile: () -> File
): DataStore<Preferences>
```

Create an instance of `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>? = null` | The corruptionHandler is invoked if DataStore encounters a `https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException` when attempting to read data. CorruptionExceptions are thrown by serializers when data cannot be de-serialized. |
| `migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>> = listOf()` | 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.) |
| `scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html = CoroutineScope(Dispatchers.IO + SupervisorJob())` | The scope in which IO operations and transform functions will execute. |
| `produceFile: () -> https://developer.android.com/reference/java/io/File.html` | 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/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |

### create

Cmn android N JS

```
fun create(
    storage: Storage<Preferences>,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(ioDispatcher() + SupervisorJob())
): DataStore<Preferences>
```

Create an instance of `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `storage: https://developer.android.com/reference/kotlin/androidx/datastore/core/Storage<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | The storage object defines where and how the preferences will be stored. |
| `corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>? = null` | The corruptionHandler is invoked if DataStore encounters a `https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException` when attempting to read data. CorruptionExceptions are thrown by serializers when data cannot be de-serialized. |
| `migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>> = listOf()` | 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.) |
| `scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html = CoroutineScope(ioDispatcher() + SupervisorJob())` | 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/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |

### createWithPath

Cmn android N JS

```
fun createWithPath(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(ioDispatcher() + SupervisorJob()),
    produceFile: () -> Path
): DataStore<Preferences>
```

Create an instance of `https://developer.android.com/reference/kotlin/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 |
|---|---|
| `corruptionHandler: https://developer.android.com/reference/kotlin/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>? = null` | The corruptionHandler is invoked if DataStore encounters a `https://developer.android.com/reference/kotlin/androidx/datastore/core/CorruptionException` when attempting to read data. CorruptionExceptions are thrown by serializers when data cannot be de-serialized. |
| `migrations: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.collections/-list/index.html<https://developer.android.com/reference/kotlin/androidx/datastore/core/DataMigration<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>> = listOf()` | 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.) |
| `scope: https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html = CoroutineScope(ioDispatcher() + SupervisorJob())` | 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. |
| `produceFile: () -> https://lysine.dev/okio/3.x/okio/okio/-path/index.html` | 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/kotlin/androidx/datastore/core/DataStore<https://developer.android.com/reference/kotlin/androidx/datastore/preferences/core/Preferences>` | a new DataStore instance with the provided configuration |