PreferenceDataStoreFactory

object PreferenceDataStoreFactory


Public factory for creating PreferenceDataStore instances.

Summary

Public methods

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

Create an instance of SingleProcessDataStore.

Public methods

create

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

Create an instance of SingleProcessDataStore. 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
@Nullable 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.

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

Returns
DataStore<@NonNull Preferences>

a new DataStore instance with the provided configuration