RxPreferenceDataStoreDelegateKt

public final class RxPreferenceDataStoreDelegateKt


Summary

Public methods

static final @NonNull ReadOnlyProperty<@NonNull Context, @NonNull RxDataStore<@NonNull Preferences>>
rxPreferencesDataStore(
    String name,
    ReplaceFileCorruptionHandler<Preferences> corruptionHandler,
    Function1<ContextList<DataMigration<Preferences>>> produceMigrations,
    Scheduler scheduler
)

Creates a property delegate for a single process Preferences DataStore.

Public methods

rxPreferencesDataStore

public static final @NonNull ReadOnlyProperty<@NonNull Context, @NonNull RxDataStore<@NonNull Preferences>> rxPreferencesDataStore(
    String name,
    ReplaceFileCorruptionHandler<Preferences> corruptionHandler,
    Function1<ContextList<DataMigration<Preferences>>> produceMigrations,
    Scheduler scheduler
)

Creates a property delegate for a single process Preferences 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.myRxDataStore by rxPreferencesDataStore("filename", serializer)

class SomeClass(val context: Context) {
fun update(): Single<Preferences> = context.myRxDataStore.updateDataAsync {...}
}
Parameters
String name

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.

ReplaceFileCorruptionHandler<Preferences> corruptionHandler

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.

Function1<ContextList<DataMigration<Preferences>>> produceMigrations

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 be