GuavaDataStore.Builder


class GuavaDataStore.Builder<T : Any>


Builder class for a GuavaDataStore.

Summary

Public constructors

<T : Any> Builder(serializer: Serializer<T>, produceFile: Callable<File>)
<T : Any> Builder(
    context: Context,
    fileName: String,
    serializer: Serializer<T>
)

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

Public functions

GuavaDataStore.Builder<T>
addDataMigration(dataMigration: DataMigration<T>)

Add a DataMigration to the Datastore.

GuavaDataStore<T>

Build the DataStore.

GuavaDataStore.Builder<T>

Flag used to signal the creation of a multi-process DataStore using a MultiProcessCoordinator.

GuavaDataStore.Builder<T>
setCorruptionHandler(
    corruptionHandler: ReplaceFileCorruptionHandler<T>
)

Sets the corruption handler to install into the DataStore.

GuavaDataStore.Builder<T>
setExecutor(executor: Executor)

Sets the Executor used by the DataStore.

Public constructors

Builder

<T : Any> Builder(serializer: Serializer<T>, produceFile: Callable<File>)

Builder

<T : Any> Builder(
    context: Context,
    fileName: String,
    serializer: Serializer<T>
)

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

Either produceFile or context & name must be set, but not both. This is enforced by the two constructors.

Parameters
context: Context

the Context from which we retrieve files directory.

fileName: String

the filename relative to Context.applicationContext.filesDir that DataStore acts on. The File is obtained from dataStoreFile. It is created in the "/datastore" subdirectory.

serializer: Serializer<T>

the Serializer for the type that this DataStore acts on.

Public functions

addDataMigration

Added in 1.2.0-alpha02
fun addDataMigration(dataMigration: DataMigration<T>): GuavaDataStore.Builder<T>

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

Parameters
dataMigration: DataMigration<T>

the migration to add

Returns
GuavaDataStore.Builder<T>

this

build

Added in 1.2.0-alpha02
fun build(): GuavaDataStore<T>

Build the DataStore.

Returns
GuavaDataStore<T>

the DataStore with the provided parameters

enableMultiProcess

Added in 1.2.0-alpha02
fun enableMultiProcess(): GuavaDataStore.Builder<T>

Flag used to signal the creation of a multi-process DataStore using a MultiProcessCoordinator.

By default, the builder sets up a DataStore intended for single-application use. It prioritizes speed and is created using DataStoreFactory. However, if you anticipate any scenario where different parts of your system (even if they are just reading data) might access the DataStore at the same time from separate processes, the enableMultiProcess flag must be set to true. This ensures a MultiProcessDataStoreFactory is used, providing the necessary safeguards for concurrent access across multiple processes.

Returns
GuavaDataStore.Builder<T>

this

setCorruptionHandler

Added in 1.2.0-alpha02
fun setCorruptionHandler(
    corruptionHandler: ReplaceFileCorruptionHandler<T>
): GuavaDataStore.Builder<T>

Sets the corruption handler to install into the DataStore.

This parameter is optional and defaults to no corruption handler.

Parameters
corruptionHandler: ReplaceFileCorruptionHandler<T>

the handler to invoke when there is a file corruption

Returns
GuavaDataStore.Builder<T>

this

setExecutor

Added in 1.2.0-alpha02
fun setExecutor(executor: Executor): GuavaDataStore.Builder<T>

Sets the Executor used by the DataStore.

This parameter is optional and defaults to Dispatchers.IO.

Parameters
executor: Executor

the executor to be used by DataStore

Returns
GuavaDataStore.Builder<T>

this