# DataStore.Builder

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

*** ** * ** ***


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

<br />

*** ** * ** ***

Builder for `https://developer.android.com/reference/androidx/datastore/core/DataStore`.

## Summary

| ### Public constructors |
|---|
| `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder#Builder(androidx.datastore.core.Storage,kotlin.coroutines.CoroutineContext)( @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://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html context )` |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder#addMigrations(kotlin.collections.List)(@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)` Adds `https://developer.android.com/reference/androidx/datastore/core/DataMigration`s to the 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 T>` | `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder#build()()` Validates the configuration and builds the `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder` instance. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder#setCorruptionHandler(androidx.datastore.core.CorruptionHandler)(@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> handler)` Sets the `https://developer.android.com/reference/androidx/datastore/core/CorruptionHandler` for the DataStore. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder#setTracer(androidx.tracing.Tracer)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/tracing/Tracer tracer)` Sets the `https://developer.android.com/reference/androidx/tracing/Tracer` for Android. |

## Public constructors

### Builder

```
public <T extends Object> Builder(
    @NonNull Storage<@NonNull T> storage,
    @NonNull CoroutineContext context
)
```

## Public methods

### addMigrations

```
public final @NonNull DataStore.Builder<@NonNull T> addMigrations(@NonNull List<@NonNull DataMigration<@NonNull T>> migrations)
```

Adds `https://developer.android.com/reference/androidx/datastore/core/DataMigration`s to the DataStore.

Migrations are run in the order they are added before any data is returned to the user.

| Parameters |
|---|---|
| `@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` | the list of migrations. |

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

### build

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

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

Validates the configuration and builds the `https://developer.android.com/reference/androidx/datastore/core/DataStore.Builder` instance.

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

### setCorruptionHandler

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

```
public final @NonNull DataStore.Builder<@NonNull T> setCorruptionHandler(@NonNull CorruptionHandler<@NonNull T> handler)
```

Sets the `https://developer.android.com/reference/androidx/datastore/core/CorruptionHandler` for the DataStore.

This handler is invoked if the `https://developer.android.com/reference/androidx/datastore/core/Storage` layer throws a `https://developer.android.com/reference/androidx/datastore/core/CorruptionException`. Defaults to `https://developer.android.com/reference/androidx/datastore/core/handlers/ReThrowCorruptionHandler`.

| Parameters |
|---|---|
| `@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> handler` | the corruption handler. |

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

### setTracer

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

```
public final @NonNull DataStore.Builder<@NonNull T> setTracer(@NonNull Tracer tracer)
```

Sets the `https://developer.android.com/reference/androidx/tracing/Tracer` for Android.

| Parameters |
|---|---|
| `@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` to use. |

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