Preferences DataStore provides an edit() function that transactionally updates the data in a DataStore. For example, this function's transform parameter accepts a block of code where you can update the values as needed.
suspend fun incrementCounter() {
context.dataStore.edit { settings ->
val currentCounterValue = settings[EXAMPLE_COUNTER] ?: 0
settings[EXAMPLE_COUNTER] = currentCounterValue + 1
// can we track success here? i am afraid code might execute asynchronously
}
I'd like to track when it successfully writes to data store. Where should I put the tracking code?