O Google tem o compromisso de promover a igualdade racial para as comunidades negras. Saiba como.


Top-level functions summary

SharedPreferencesMigration(context: Context, sharedPreferencesName: String, keysToMigrate: Set<String>? = MIGRATE_ALL_KEYS, deleteEmptyPreferences: Boolean = true)

Creates a SharedPreferencesMigration for DataStore.

Extension functions summary

For android.content.Context
Context.createDataStore(name: String, corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null, migrations: List<DataMigration<Preferences>> = listOf(), scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()))

Create an instance of SingleProcessDataStore.

Top-level functions


@JvmOverloads fun SharedPreferencesMigration(
    context: Context,
    sharedPreferencesName: String,
    keysToMigrate: Set<String>? = MIGRATE_ALL_KEYS,
    deleteEmptyPreferences: Boolean = true
): SharedPreferencesMigration<Preferences>

Creates a SharedPreferencesMigration for DataStore.

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.
deleteEmptyPreferences: Boolean = true

If enabled and the SharedPreferences are empty (i.e. no remaining keys) after this migration runs, the leftover SharedPreferences file is deleted. Note that this cleanup runs only if the migration itself runs, i.e., if the keys were never in SharedPreferences to begin with then the (potentially) empty SharedPreferences won't be cleaned up by this option. This functionality is best effort - if there is an issue deleting the SharedPreferences file it will be silently ignored.

TODO(rohitsat): determine whether to remove the deleteEmptyPreferences option.

Extension functions


fun Context.createDataStore(
    name: String,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
): DataStore<Preferences>

Create an instance of SingleProcessDataStore. Never create more than one instance of DataStore for a given file; doing so can break all DataStore functionality. You should consider managing your DataStore instance as a singleton. Note: this function is not comparable to Context.getSharedPreferences; this function creates and returns a new instance of DataStore each time it is called.

name: String The name of the preferences. The preferences will be stored in a file obtained by calling: File(context.filesDir, "datastore/" + name + ".preferences_pb")
corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null The corruptionHandler is invoked if DataStore encounters a CorruptionException when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized.
migrations: List<DataMigration<Preferences>> = listOf() 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.
a new DataStore instance with the provided configuration