# GuavaDataStore.Builder

Artifact: [androidx.datastore:datastore-guava](https://developer.android.com/jetpack/androidx/releases/datastore) [View Source](https://cs.android.com/search?q=file:androidx/datastore/guava/GuavaDataStore.kt+class:androidx.datastore.guava.GuavaDataStore.Builder) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

*** ** * ** ***

<br />

```
public final class GuavaDataStore.Builder<T extends Object>
```

<br />

*** ** * ** ***

Builder class for a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore`.

## Summary

| ### Public constructors |
|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#Builder(androidx.datastore.core.Serializer,java.util.concurrent.Callable)( @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/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Callable.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/io/File.html> produceFile )` |
| `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#Builder(android.content.Context,kotlin.String,androidx.datastore.core.Serializer)( @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 )` Create a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder` with the `https://developer.android.com/reference/android/content/Context.html` and name from which to derive the `https://developer.android.com/reference/androidx/datastore/core/DataStore` file. |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#addDataMigration(androidx.datastore.core.DataMigration)(@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> dataMigration)` Add a DataMigration to the Datastore. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#build()()` Build the DataStore. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#enableMultiProcess()()` Flag used to signal the creation of a multi-process DataStore using a `https://developer.android.com/reference/androidx/datastore/core/MultiProcessCoordinator`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#setCorruptionHandler(androidx.datastore.core.handlers.ReplaceFileCorruptionHandler)( @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> corruptionHandler )` Sets the corruption handler to install into the DataStore. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#setExecutor(java.util.concurrent.Executor)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor)` Sets the `https://developer.android.com/reference/java/util/concurrent/Executor.html` used by the DataStore. |

## Public constructors

### Builder

```
public <T extends Object> Builder(
    @NonNull Serializer<@NonNull T> serializer,
    @NonNull Callable<@NonNull File> produceFile
)
```

### Builder

```
public <T extends Object> Builder(
    @NonNull Context context,
    @NonNull String fileName,
    @NonNull Serializer<@NonNull T> serializer
)
```

Create a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder` with the `https://developer.android.com/reference/android/content/Context.html` and name from which to derive the `https://developer.android.com/reference/androidx/datastore/core/DataStore` file. The file is generated by File(this.filesDir, "datastore/$fileName"). The user is responsible for ensuring that there is never more than one DataStore acting on a file at a time.

Either produceFile or context \& name must be set, but not both. This is enforced by the two constructors.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/android/content/Context.html context` | the `https://developer.android.com/reference/android/content/Context.html` from which we retrieve files directory. |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html fileName` | the filename relative to `https://developer.android.com/reference/android/content/Context.html#filesDir` that `https://developer.android.com/reference/androidx/datastore/core/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. |
| `@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 `https://developer.android.com/reference/androidx/datastore/core/Serializer` for the type that this DataStore acts on. |

## Public methods

### addDataMigration

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

```
public final @NonNull GuavaDataStore.Builder<@NonNull T> addDataMigration(@NonNull DataMigration<@NonNull T> dataMigration)
```

Add a DataMigration to the Datastore. Migrations are run in the order they are added.

| Parameters |
|---|---|
| `@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> dataMigration` | the migration to add |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | this |

### build

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

```
public final @NonNull GuavaDataStore<@NonNull T> build()
```

Build the DataStore.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | the DataStore with the provided parameters |

### enableMultiProcess

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

```
public final @NonNull GuavaDataStore.Builder<@NonNull T> enableMultiProcess()
```

Flag used to signal the creation of a multi-process DataStore using a `https://developer.android.com/reference/androidx/datastore/core/MultiProcessCoordinator`.

By default, the builder sets up a `https://developer.android.com/reference/androidx/datastore/core/DataStore` intended for single-application use. It prioritizes speed and is created using `https://developer.android.com/reference/androidx/datastore/core/DataStoreFactory`. However, if you anticipate any scenario where different parts of your system (even if they are just reading data) might access the `https://developer.android.com/reference/androidx/datastore/core/DataStore` at the same time from separate processes, the `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder#enableMultiProcess()` flag must be set to `true`. This ensures a `https://developer.android.com/reference/androidx/datastore/core/MultiProcessDataStoreFactory` is used, providing the necessary safeguards for concurrent access across multiple processes.

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | this |

### setCorruptionHandler

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

```
public final @NonNull GuavaDataStore.Builder<@NonNull T> setCorruptionHandler(
    @NonNull ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler
)
```

Sets the corruption handler to install into the DataStore.

This parameter is optional and defaults to no corruption handler.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/handlers/ReplaceFileCorruptionHandler<@https://developer.android.com/reference/androidx/annotation/NonNull T> corruptionHandler` | the handler to invoke when there is a file corruption |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | this |

### setExecutor

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

```
public final @NonNull GuavaDataStore.Builder<@NonNull T> setExecutor(@NonNull Executor executor)
```

Sets the `https://developer.android.com/reference/java/util/concurrent/Executor.html` used by the DataStore.

This parameter is optional and defaults to `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html`.

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | the executor to be used by `https://developer.android.com/reference/androidx/datastore/core/DataStore` |

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | this |