PreferenceDataStoreFactory


Summary

Public functions

DataStore<Preferences>
create(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
    migrations: List<DataMigration<Preferences>>,
    scope: CoroutineScope,
    produceFile: () -> File
)

Create an instance of DataStore.

android
DataStore<Preferences>
create(
    storage: Storage<Preferences>,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
    migrations: List<DataMigration<Preferences>>,
    scope: CoroutineScope
)

Create an instance of DataStore.

Cmn
android
DataStore<Preferences>
createWithPath(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>?,
    migrations: List<DataMigration<Preferences>>,
    scope: CoroutineScope,
    produceFile: () -> Path
)

Create an instance of DataStore using an okio Path.

Cmn
android

Public functions

create

fun create(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob()),
    produceFile: () -> File
): DataStore<Preferences>

Create an instance of DataStore. 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.

Parameters
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 cannot 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.

produceFile: () -> File

Function which returns the file that the new DataStore will act on. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. File will be created if it doesn't exist.

Returns
DataStore<Preferences>

a new DataStore instance with the provided configuration

create

fun create(
    storage: Storage<Preferences>,
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(ioDispatcher() + SupervisorJob())
): DataStore<Preferences>

Create an instance of DataStore. 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.

Parameters
storage: Storage<Preferences>

The storage object defines where and how the preferences will be stored.

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 cannot 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(ioDispatcher() + SupervisorJob())

The scope in which IO operations and transform functions will execute. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb.

Returns
DataStore<Preferences>

a new DataStore instance with the provided configuration

createWithPath

fun createWithPath(
    corruptionHandler: ReplaceFileCorruptionHandler<Preferences>? = null,
    migrations: List<DataMigration<Preferences>> = listOf(),
    scope: CoroutineScope = CoroutineScope(ioDispatcher() + SupervisorJob()),
    produceFile: () -> Path
): DataStore<Preferences>

Create an instance of DataStore using an okio Path. 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.

Parameters
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 cannot 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(ioDispatcher() + SupervisorJob())

The scope in which IO operations and transform functions will execute. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb.

produceFile: () -> Path

Function which returns the file that the new DataStore will act on. The function must return the same path every time. No two instances of PreferenceDataStore should act on the same file at the same time. The file must have the extension preferences_pb. File will be created if it doesn't exist.

Returns
DataStore<Preferences>

a new DataStore instance with the provided configuration