首选项 DataStore 提供了一个 edit() 函数,该函数以事务方式更新 DataStore 中的数据。例如,此函数的 transform 参数接受一段代码,您可以在其中根据需要更新值。
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
}
我想跟踪它何时成功写入数据存储。我应该把跟踪代码放在哪里?