# PreferencesKt

Added in 1.0.0

*** ** * ** ***


```
public final class PreferencesKt
```

<br />

*** ** * ** ***

## Summary

| ### Public methods |
|---|---|
| `static final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences` | `https://developer.android.com/reference/androidx/datastore/preferences/core/PreferencesKt#(androidx.datastore.core.DataStore).edit(kotlin.coroutines.SuspendFunction1)( @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 https://developer.android.com/reference/androidx/datastore/preferences/core/Preferences> receiver, @https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> transform )` Edit the value in DataStore transactionally in an atomic read-modify-write operation. |

## Public methods

### edit

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/Preferences.kt+function:edit)

```
public static final @NonNull Preferences edit(
    @NonNull DataStore<@NonNull Preferences> receiver,
    @NonNull SuspendFunction1<@NonNull MutablePreferences, Unit> transform
)
```

Edit the value in DataStore transactionally in an atomic read-modify-write operation. All operations are serialized.

The coroutine completes when the data has been persisted durably to disk (after which `https://developer.android.com/reference/androidx/datastore/core/DataStore#data()` will reflect the update). If the transform or write to disk fails, the transaction is aborted and an exception is thrown.

Note: values that are changed in `https://developer.android.com/reference/androidx/datastore/preferences/core/package-summary#(androidx.datastore.core.DataStore).edit(kotlin.coroutines.SuspendFunction1)` are NOT updated in DataStore until after the transform completes. Do not assume that the data has been successfully persisted until after edit returns successfully.

Note: do NOT store a reference to the MutablePreferences provided to transform. Mutating this after `https://developer.android.com/reference/androidx/datastore/preferences/core/package-summary#(androidx.datastore.core.DataStore).edit(kotlin.coroutines.SuspendFunction1)` returns will NOT change the data in DataStore. Future versions of this may throw exceptions if the MutablePreferences object is mutated outside of `https://developer.android.com/reference/androidx/datastore/preferences/core/package-summary#(androidx.datastore.core.DataStore).edit(kotlin.coroutines.SuspendFunction1)`.

See `https://developer.android.com/reference/androidx/datastore/core/DataStore#updateData(kotlin.coroutines.SuspendFunction1)`.

Example usage: val COUNTER_KEY = intPreferencesKey("my_counter")

dataStore.edit { prefs -\> prefs\\\[COUNTER_KEY\\\] = prefs\\\[COUNTER_KEY\\\] :? 0 + 1 }

| Parameters |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull SuspendFunction1<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/datastore/preferences/core/MutablePreferences, https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html> transform` | block which accepts MutablePreferences that contains all the preferences currently in DataStore. Changes to this MutablePreferences object will be persisted once transform completes. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/datastore/core/IOException` | when an exception is encountered when writing data to disk |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-exception/index.html` | when thrown by the transform block |