# GuavaDataStore

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) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/datastore#1.2.0)

*** ** * ** ***

<br />

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

<br />

*** ** * ** ***

The class that wraps around `https://developer.android.com/reference/androidx/datastore/core/DataStore` to provide an interface that returns `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html`s for DataStore reads and writes.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore.Builder<T extends https://developer.android.com/reference/java/lang/Object.html>` Builder class for a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore`. |

| ### Public methods |
|---|---|
| `static 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>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#from(androidx.datastore.core.DataStore,kotlin.coroutines.CoroutineContext)( @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> dataStore, @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext )` Wraps a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` around a `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `static 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>` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#from(androidx.datastore.core.DataStore,java.util.concurrent.Executor)( @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> dataStore, @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor )` Wraps a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` around a `https://developer.android.com/reference/androidx/datastore/core/DataStore`. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#getDataAsync()()` Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` to get the latest persisted data. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html<@https://developer.android.com/reference/androidx/annotation/NonNull T>` | `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/core/util/Function<@https://developer.android.com/reference/androidx/annotation/NonNull T, @https://developer.android.com/reference/androidx/annotation/NonNull T> dataTransform)` Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` to update the data using the provided `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)`. |

## Public methods

### from

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

```
public static final @NonNull GuavaDataStore<@NonNull T> <T extends Object> from(
    @NonNull DataStore<@NonNull T> dataStore,
    @NonNull CoroutineContext coroutineContext
)
```

Wraps a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` around a `https://developer.android.com/reference/androidx/datastore/core/DataStore`. This method does not create a new `https://developer.android.com/reference/androidx/datastore/core/DataStore`, so all `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#getDataAsync()` and `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)` called from the resulting `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` will be sequenced by the underlying `https://developer.android.com/reference/androidx/datastore/core/DataStore`. It is thread-safe.

| Parameters |
|---|---|
| `@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> dataStore` | the DataStore used to create GuavaDataStore |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html coroutineContext` | the CoroutineContext used to launch the calls to DataStore. The default value is `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-i-o.html` |

| 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 GuavaDataStore created with the provided parameters |

### from

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

```
public static final @NonNull GuavaDataStore<@NonNull T> <T extends Object> from(
    @NonNull DataStore<@NonNull T> dataStore,
    @NonNull Executor executor
)
```

Wraps a `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` around a `https://developer.android.com/reference/androidx/datastore/core/DataStore`. This method does not create a new `https://developer.android.com/reference/androidx/datastore/core/DataStore`, so all `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#getDataAsync()` and `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)` called from the resulting `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore` will be sequenced by the underlying `https://developer.android.com/reference/androidx/datastore/core/DataStore`. It is thread-safe.

| Parameters |
|---|---|
| `@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> dataStore` | the DataStore used to create GuavaDataStore |
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/concurrent/Executor.html executor` | the Executor used to launch the calls to 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 GuavaDataStore created with the provided parameters |

### getDataAsync

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

```
public final @NonNull ListenableFuture<@NonNull T> getDataAsync()
```

Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` to get the latest persisted data. It is not blocked by any ongoing updates.

### updateDataAsync

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

```
public final @NonNull ListenableFuture<@NonNull T> updateDataAsync(@NonNull Function<@NonNull T, @NonNull T> dataTransform)
```

Returns a `https://guava.dev/releases/snapshot-android/api/docs/com/google/common/util/concurrent/ListenableFuture.html` to update the data using the provided `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)`. The `https://developer.android.com/reference/androidx/datastore/guava/GuavaDataStore#updateDataAsync(androidx.core.util.Function)` is given the latest persisted data to produce its output, which is then persisted and returned. Concurrent updates are serialized (at most one update running at a time).

Ideally the shape of the `https://developer.android.com/reference/androidx/core/util/Function` param would have been a `T -> R` but we choose to keep it as `T -> T` to match `https://developer.android.com/reference/androidx/datastore/core/DataStore#updateData(kotlin.coroutines.SuspendFunction1)`.