StorageConnection

public interface StorageConnection<T extends Object> extends Closeable


StorageConnection provides a way to read and write a particular type of data. StorageConnections are created from Storage objects.

Summary

Public methods

abstract @NonNull InterProcessCoordinator

Provides a coordinator to guarantee data consistency across multiple threads and processes.

abstract @NonNull R
<R extends Object> readScope(
    @ExtensionFunctionType @NonNull SuspendFunction2<@NonNull ReadScope<@NonNull T>, @NonNull Boolean, @NonNull R> block
)

Creates a scope for reading to allow storage reads, and will try to obtain a read lock.

abstract void
writeScope(
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull WriteScope<@NonNull T>, Unit> block
)

Creates a write scope that guaranteed to only have one single writer, ensuring also that any reads within this scope have the most current data.

Extension functions

default final @NonNull T
<T extends Object> StorageConnectionKt.readData(
    @NonNull StorageConnection<@NonNull T> receiver
)
default final void
<T extends Object> StorageConnectionKt.writeData(
    @NonNull StorageConnection<@NonNull T> receiver,
    @NonNull T value
)

Inherited methods

From androidx.datastore.core.Closeable
abstract void

Closes the specified resource.

Public methods

getCoordinator

Added in 1.1.0-beta02
abstract @NonNull InterProcessCoordinator getCoordinator()

Provides a coordinator to guarantee data consistency across multiple threads and processes.

readScope

abstract @NonNull R <R extends Object> readScope(
    @ExtensionFunctionType @NonNull SuspendFunction2<@NonNull ReadScope<@NonNull T>, @NonNull Boolean, @NonNull R> block
)

Creates a scope for reading to allow storage reads, and will try to obtain a read lock.

Parameters
@ExtensionFunctionType @NonNull SuspendFunction2<@NonNull ReadScope<@NonNull T>, @NonNull Boolean, @NonNull R> block

The block of code that is performed within this scope. Block will receive locked parameter which is true if the try lock succeeded.

Throws
androidx.datastore.core.IOException

when there is an unrecoverable exception in reading.

writeScope

Added in 1.1.0-beta02
abstract void writeScope(
    @ExtensionFunctionType @NonNull SuspendFunction1<@NonNull WriteScope<@NonNull T>, Unit> block
)

Creates a write scope that guaranteed to only have one single writer, ensuring also that any reads within this scope have the most current data.

Throws
androidx.datastore.core.IOException

when there is an unrecoverable exception in writing.

Extension functions

StorageConnectionKt.readData

default final @NonNull T <T extends Object> StorageConnectionKt.readData(
    @NonNull StorageConnection<@NonNull T> receiver
)

StorageConnectionKt.writeData

default final void <T extends Object> StorageConnectionKt.writeData(
    @NonNull StorageConnection<@NonNull T> receiver,
    @NonNull T value
)