{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

DataStoreFactory

public static class DataStoreFactory


Public factory for creating DataStore instances.

Summary

Public fields

static @NonNull DataStoreFactory

Public methods

final @NonNull DataStore<@NonNull T>
@<Error class: unknown class>
<T extends Object> create(
    @NonNull Storage<@NonNull T> storage,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull <Error class: unknown class> scope
)

Create an instance of SingleProcessDataStore.

final @NonNull DataStore<@NonNull T>
@<Error class: unknown class>
<T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull <Error class: unknown class> scope,
    @NonNull Function0<@NonNull <Error class: unknown class>> produceFile
)

Create an instance of SingleProcessDataStore.

final @NonNull DataStore<@NonNull T>
<T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull CoroutineScope scope,
    @NonNull Function0<@NonNull File> produceFile
)

Create an instance of SingleProcessDataStore.

Public fields

INSTANCE

public static @NonNull DataStoreFactory INSTANCE

Public methods

create

@<Error class: unknown class>
public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Storage<@NonNull T> storage,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull <Error class: unknown class> scope
)

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. If there are multiple DataStores active, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active.

T is the type DataStore acts on. The type T must be immutable. Mutating a type used in DataStore invalidates any guarantees that DataStore provides and will result in potentially serious, hard-to-catch bugs. We strongly recommend using protocol buffers: https://developers.google.com/protocol-buffers/docs/javatutorial - which provides immutability guarantees, a simple API and efficient serialization.

Parameters
@NonNull Storage<@NonNull T> storage

Storage for the type T used with DataStore. The type T must be immutable.

ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler

The corruptionHandler is invoked if DataStore encounters a CorruptionException when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized.

@NonNull List<@NonNull DataMigration<@NonNull T>> migrations

Migrations are run before any access to data can occur. Migrations must be idempotent.

@NonNull <Error class: unknown class> scope

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

Returns
@NonNull DataStore<@NonNull T>

a new DataStore instance with the provided configuration

create

@<Error class: unknown class>
public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> migrations,
    @NonNull <Error class: unknown class> scope,
    @NonNull Function0<@NonNull <Error class: unknown class>> 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. If there are multiple DataStores active, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active.

T is the type DataStore acts on. The type T must be immutable. Mutating a type used in DataStore invalidates any guarantees that DataStore provides and will result in potentially serious, hard-to-catch bugs. We strongly recommend using protocol buffers: https://developers.google.com/protocol-buffers/docs/javatutorial - which provides immutability guarantees, a simple API and efficient serialization.

Parameters
@NonNull Serializer<@NonNull T> serializer

Serializer for the type T used with DataStore. The type T must be immutable.

ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler

The corruptionHandler is invoked if DataStore encounters a CorruptionException when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized.

@NonNull List<@NonNull DataMigration<@NonNull T>> migrations

Migrations are run before any access to data can occur. Migrations must be idempotent.

@NonNull <Error class: unknown class> scope

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

@NonNull Function0<@NonNull <Error class: unknown class>> 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.

Returns
@NonNull DataStore<@NonNull T>

a new DataStore instance with the provided configuration

create

public final @NonNull DataStore<@NonNull T> <T extends Object> create(
    @NonNull Serializer<@NonNull T> serializer,
    ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler,
    @NonNull List<@NonNull DataMigration<@NonNull T>> 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. If there are multiple DataStores active, DataStore will throw IllegalStateException when reading or updating data. A DataStore is considered active as long as its scope is active.

T is the type DataStore acts on. The type T must be immutable. Mutating a type used in DataStore invalidates any guarantees that DataStore provides and will result in potentially serious, hard-to-catch bugs. We strongly recommend using protocol buffers: https://developers.google.com/protocol-buffers/docs/javatutorial - which provides immutability guarantees, a simple API and efficient serialization.

Parameters
@NonNull Serializer<@NonNull T> serializer

Serializer for the type T used with DataStore. The type T must be immutable.

ReplaceFileCorruptionHandler<@NonNull T> corruptionHandler

The corruptionHandler is invoked if DataStore encounters a CorruptionException when attempting to read data. CorruptionExceptions are thrown by serializers when data can not be de-serialized.

@NonNull List<@NonNull DataMigration<@NonNull T>> migrations

Migrations are run before any access to data can occur. Migrations must be idempotent.

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

Returns
@NonNull DataStore<@NonNull T>

a new DataStore instance with the provided configuration