RxPreferenceDataStoreBuilder

public final class RxPreferenceDataStoreBuilder


Builder for a Preferences RxDataStore that works on a single process.

Summary

Public constructors

Create a RxPreferenceDataStoreBuilder with the callable which returns the File that DataStore acts on.

Create a RxPreferenceDataStoreBuilder with the Context and name from which to derive the DataStore file.

Public methods

final @NonNull RxPreferenceDataStoreBuilder

Add a DataMigration to the Datastore.

final @NonNull RxPreferenceDataStoreBuilder

Add an RxDataMigration to the DataStore.

final @NonNull RxDataStore<@NonNull Preferences>

Build the DataStore.

final @NonNull RxPreferenceDataStoreBuilder

Sets the corruption handler to install into the DataStore.

final @NonNull RxPreferenceDataStoreBuilder

Set the Scheduler on which to perform IO and transform operations.

Public constructors

RxPreferenceDataStoreBuilder

public final RxPreferenceDataStoreBuilder(Callable<File> produceFile)

Create a RxPreferenceDataStoreBuilder with the callable which returns the File that DataStore acts on. The user is responsible for ensuring that there is never more than one DataStore acting on a file at a time.

Parameters
Callable<File> produceFile

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 DataStore should act on the same file at the same time.

RxPreferenceDataStoreBuilder

public final RxPreferenceDataStoreBuilder(Context context, String name)

Create a RxPreferenceDataStoreBuilder with the Context and name from which to derive the DataStore file. The file is generated by calling File(context.filesDir, "datastore/" + name + ".preferences_pb"). The user is responsible for ensuring that there is never more than one DataStore acting on a file at a time.

Parameters
Context context

the context from which we retrieve files directory.

String name

the filename relative to Context.filesDir that DataStore acts on. The File is obtained by calling File(this.filesDir, "datastore/$name.preferences_pb"). No two instances of DataStore should act on the same file at the same time.

Public methods

addDataMigration

public final @NonNull RxPreferenceDataStoreBuilder addDataMigration(DataMigration<Preferences> dataMigration)

Add a DataMigration to the Datastore. Migrations are run in the order they are added.

Parameters
DataMigration<Preferences> dataMigration

the migration to add

addRxDataMigration

public final @NonNull RxPreferenceDataStoreBuilder addRxDataMigration(RxDataMigration<Preferences> rxDataMigration)

Add an RxDataMigration to the DataStore. Migrations are run in the order they are added.

Parameters
RxDataMigration<Preferences> rxDataMigration

the migration to add.

build

public final @NonNull RxDataStore<@NonNull Preferencesbuild()

Build the DataStore.

Returns
@NonNull RxDataStore<@NonNull Preferences>

the DataStore with the provided parameters

Throws
kotlin.IllegalStateException

if serializer is not set or if neither produceFile not context and name are set.

setCorruptionHandler

public final @NonNull RxPreferenceDataStoreBuilder setCorruptionHandler(
    ReplaceFileCorruptionHandler<Preferences> corruptionHandler
)

Sets the corruption handler to install into the DataStore.

This parameter is optional and defaults to no corruption handler.

Parameters
ReplaceFileCorruptionHandler<Preferences> corruptionHandler

the ReplaceFileCorruptionHandler to install

setIoScheduler

public final @NonNull RxPreferenceDataStoreBuilder setIoScheduler(Scheduler ioScheduler)

Set the Scheduler on which to perform IO and transform operations. This is converted into a CoroutineDispatcher before being added to DataStore.

This parameter is optional and defaults to Schedulers.io().

Parameters
Scheduler ioScheduler

the scheduler on which IO and transform operations are run