PreferenceDataStoreFactory


public static class PreferenceDataStoreFactory


Summary

Public methods

final @NonNull DataStore<@NonNull Preferences>
create(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)

Create an instance of DataStore.

final @NonNull DataStore<@NonNull Preferences>
create(
    @NonNull Storage<@NonNull Preferences> storage,
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope
)

Create an instance of DataStore.

final @NonNull DataStore<@NonNull Preferences>
createWithPath(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull Path> produceFile
)

Create an instance of DataStore using an okio Path.

Public fields

INSTANCE

Added in 1.0.0
public @NonNull PreferenceDataStoreFactory INSTANCE

Public methods

create

public final @NonNull DataStore<@NonNull Preferencescreate(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)

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
ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler

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.

@NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations

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.)

@NonNull CoroutineScope scope

The scope in which IO operations and transform functions will execute.

@NonNull Function0<@NonNull 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 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
@NonNull DataStore<@NonNull Preferences>

a new DataStore instance with the provided configuration

create

public final @NonNull DataStore<@NonNull Preferencescreate(
    @NonNull Storage<@NonNull Preferences> storage,
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope
)

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
@NonNull Storage<@NonNull Preferences> storage

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

ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler

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.

@NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations

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.)

@NonNull CoroutineScope scope

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
@NonNull DataStore<@NonNull Preferences>

a new DataStore instance with the provided configuration

createWithPath

public final @NonNull DataStore<@NonNull PreferencescreateWithPath(
    ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull Path> produceFile
)

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
ReplaceFileCorruptionHandler<@NonNull Preferences> corruptionHandler

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.

@NonNull List<@NonNull DataMigration<@NonNull Preferences>> migrations

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.)

@NonNull CoroutineScope scope

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.

@NonNull Function0<@NonNull Path> 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 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
@NonNull DataStore<@NonNull Preferences>

a new DataStore instance with the provided configuration