androidandroid-preferencesdatastore

Multi process PreferencesDataStore with more than one field


The example that Google give for the Multi-process version of DataStore includes just one field in settings:

data class Settings(
   val lastUpdate: Long
)

Then the update code is:

scope.launch {
   dataStore.updateData {
       Settings(lastUpdate = System.currentTimeMillis())
   }
}

However, this only works if I have one field. What happens when I have two fields? For example, my settings:

data class Settings(

    val bypassEnabled: Boolean,
    val clientMode: ProvisionClientMode
)

It won't let me do this:

datastore.updateData {Settings(clientMode = ProvisionClientMode.ACTIVE_MODE) }

because it wants values for all the fields.

I could do a "read" and clone it with a change but that just seems wrong.

Am I missing something here?


Solution

  • dataStore.updateData { currentSettings ->
         currentSettings.copy(clientMode = ProvisionClientMode.ACTIVE_MODE)
     }
    

    I think it can be solved by copying the existing one.