# MultiProcessDataStoreFactory

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/MultiProcessDataStoreFactory.android.kt+class:androidx.datastore.core.MultiProcessDataStoreFactory) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.1.0)

*** ** * ** ***


```
public static class MultiProcessDataStoreFactory
```

<br />

*** ** * ** ***

## Summary

| ### Public fields |
|---|---|
| `static @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/MultiProcessDataStoreFactory` | `https://developer.android.com/reference/androidx/datastore/core/MultiProcessDataStoreFactory#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/MultiProcessDataStoreFactory#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 )` Create an instance of MultiProcessDataStore, which provides cross-process eventual consistency. |
| `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/MultiProcessDataStoreFactory#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 MultiProcessDataStore, which provides cross-process eventual consistency. |

## Public fields

### INSTANCE

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

```
public static @NonNull MultiProcessDataStoreFactory 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 an instance of MultiProcessDataStore, which provides cross-process eventual consistency. Never create more than one instance of DataStore for a given file in the same process; doing so can break all DataStore functionality. You should consider managing your DataStore instance for each file as a singleton. If there are multiple DataStores active for a given file in the same process, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active. Having multiple instances, each for a different file, in the same process is OK.

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/Storage<@https://developer.android.com/reference/androidx/annotation/NonNull T> storage` | Storage to handle file reads and writes used with DataStore. The type T must be immutable. The storage must operate on the same file as the one passed in {@link produceFile}. |
| `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> corruptionHandler` | The `https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler` 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 |

### 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 MultiProcessDataStore, which provides cross-process eventual consistency. Never create more than one instance of DataStore for a given file in the same process; doing so can break all DataStore functionality. You should consider managing your DataStore instance for each file as a singleton. If there are multiple DataStores active for a given file in the same process, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active. Having multiple instances, each for a different file, in the same process is OK.

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 {@link androidx.datastore.core.handlers.ReplaceFileCorruptionHandler} 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 in the same process. |

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