Google is committed to advancing racial equity for Black communities. See how.

androidx.datastore

Interfaces

CorruptionHandler

CorruptionHandlers allow recovery from corruption that prevents reading data from the file (as indicated by a CorruptionException).

DataMigration

Interface for migrations to DataStore.

DataStore

DataStore provides a safe and durable way to store small amounts of data, such as preferences and application state.

Serializer

The serializer determines the on-disk format and API for accessing it.

Exceptions

CorruptionException

A subclass of IOException that indicates that the file could not be de-serialized due to data format corruption.

Extension functions summary

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

Create an instance of SingleProcessDataStore.

Extension functions

createDataStore

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

Create an instance of SingleProcessDataStore. The user is responsible for ensuring that there is never more than one instance of SingleProcessDataStore acting on a file at a time.

Parameters
fileName: String the filename relative to Context.filesDir that DataStore acts on. The File is obtained by calling File(context.filesDir, fileName). No two instances of DataStore should act on the same file at the same time.
corruptionHandler: ReplaceFileCorruptionHandler<T>? = 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<T>> = 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.