androidx.datastore.preferences


Top-level functions summary

SharedPreferencesMigration<Preferences>
SharedPreferencesMigration(
    produceSharedPreferences: () -> SharedPreferences,
    keysToMigrate: Set<String>
)

Creates a SharedPreferencesMigration for DataStore.

SharedPreferencesMigration<Preferences>
SharedPreferencesMigration(
    context: Context,
    sharedPreferencesName: String,
    keysToMigrate: Set<String>
)

Creates a SharedPreferencesMigration for DataStore.

ReadOnlyProperty<ContextDataStore<Preferences>>
preferencesDataStore(
    name: String,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
    produceMigrations: (Context) -> List<DataMigration<Preferences>>,
    scope: CoroutineScope
)

Creates a property delegate for a single process DataStore.

Extension functions summary

File

Generate the File object for Preferences DataStore based on the provided context and name.

Top-level functions

SharedPreferencesMigration

fun SharedPreferencesMigration(
    produceSharedPreferences: () -> SharedPreferences,
    keysToMigrate: Set<String> = MIGRATE_ALL_KEYS
): SharedPreferencesMigration<Preferences>

Creates a SharedPreferencesMigration for DataStore.

Note: This migration only supports the basic SharedPreferences types: boolean, float, int, long, string and string set. If the result of getAll contains other types, they will be ignored.

Parameters
produceSharedPreferences: () -> SharedPreferences

Should return the instance of SharedPreferences to migrate from.

keysToMigrate: Set<String> = MIGRATE_ALL_KEYS

The list of keys to migrate. The keys will be mapped to datastore.Preferences with their same values. If the key is already present in the new Preferences, the key will not be migrated again. If the key is not present in the SharedPreferences it will not be migrated. If keysToMigrate is not set, all keys will be migrated from the existing SharedPreferences.

SharedPreferencesMigration

fun SharedPreferencesMigration(
    context: Context,
    sharedPreferencesName: String,
    keysToMigrate: Set<String> = MIGRATE_ALL_KEYS
): SharedPreferencesMigration<Preferences>

Creates a SharedPreferencesMigration for DataStore.

If the SharedPreferences is empty once the migration completes, this migration will attempt to delete it.

Parameters
context: Context

Context used for getting SharedPreferences.

sharedPreferencesName: String

The name of the SharedPreferences.

keysToMigrate: Set<String> = MIGRATE_ALL_KEYS

The list of keys to migrate. The keys will be mapped to datastore.Preferences with their same values. If the key is already present in the new Preferences, the key will not be migrated again. If the key is not present in the SharedPreferences it will not be migrated. If keysToMigrate is not set, all keys will be migrated from the existing SharedPreferences.

preferencesDataStore

fun preferencesDataStore(
    name: String,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    produceMigrations: (Context) -> List<DataMigration<Preferences>> = { listOf() },
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): ReadOnlyProperty<ContextDataStore<Preferences>>

Creates a property delegate for a single process DataStore. This should only be called once in a file (at the top level), and all usages of the DataStore should use a reference the same Instance. The receiver type for the property delegate must be an instance of Context.

This should only be used from a single application in a single classloader in a single process.

Example usage:

val Context.myDataStore by preferencesDataStore("filename")

class SomeClass(val context: Context) {
suspend fun update() = context.myDataStore.edit {...}
}
Parameters
name: String

The name of the preferences. The preferences will be stored in a file in the "datastore/" subdirectory in the application context's files directory and is generated using preferencesDataStoreFile.

corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null

The corruptionHandler is invoked if DataStore encounters a androidx.datastore.core.CorruptionException when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized.

produceMigrations: (Context) -> List<DataMigration<Preferences>> = { listOf() }

produce the migrations. The ApplicationContext is passed in to these callbacks as a parameter. DataMigrations are run before any access to data can occur. Each producer and migration may be run more than once whether or not it already succeeded (potentially because another migration failed or a write to disk failed.)

scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())

The scope in which IO operations and transform functions will execute.

Returns
ReadOnlyProperty<ContextDataStore<Preferences>>

a property delegate that manages a datastore as a singleton.

Extension functions

preferencesDataStoreFile

fun Context.preferencesDataStoreFile(name: String): File

Generate the File object for Preferences DataStore based on the provided context and name. The file is in the this.applicationContext.filesDir + "datastore/" subdirectory with name. This is public to allow for testing and backwards compatibility (e.g. if moving from the preferencesDataStore delegate or context.createDataStore to PreferencesDataStoreFactory).

Do NOT use the file outside of DataStore.

the context of the application used to get the files directory

the name of the preferences