RoomDatabaseKt

Added in 3.0.0-alpha01

public final class RoomDatabaseKt


Summary

Public methods

static final @NonNull R
<R extends Object> useReaderConnection(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block
)

Acquires a READ connection, suspending while waiting if none is available and then calling the block to use the connection once it is acquired.

static final @NonNull R
<R extends Object> useWriterConnection(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block
)

Acquires a WRITE connection, suspending while waiting if none is available and then calling the block to use the connection once it is acquired.

static final @NonNull R
<R extends Object> withReadTransaction(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull TransactionScope<@NonNull R>, @NonNull R> block
)

Acquire a READ connection and start a Transactor.SQLiteTransactionType.DEFERRED transaction to execute the given block within the transaction.

static final @NonNull R
<R extends Object> withWriteTransaction(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull TransactionScope<@NonNull R>, @NonNull R> block
)

Acquire a WRITE connection and start a Transactor.SQLiteTransactionType.IMMEDIATE transaction to execute the given block within the transaction.

Public methods

useReaderConnection

public static final @NonNull R <R extends Object> useReaderConnection(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block
)

Acquires a READ connection, suspending while waiting if none is available and then calling the block to use the connection once it is acquired. A RoomDatabase will have one or more READ connections. The connection to use in the block is an instance of Transactor that provides the capabilities for performing nested transactions.

Using the connection after block completes is prohibited.

The connection will be confined to the coroutine on which block executes, attempting to use the connection from a different coroutine will result in an error.

If the current coroutine calling this function already has a confined connection, then that connection is used.

A connection is a limited resource and should not be held for more than it is needed. The best practice in using connections is to avoid executing long-running computations within the block. If a caller has to wait too long to acquire a connection a SQLiteException will be thrown due to a timeout.

Parameters
@NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block

The code to use the connection.

Throws
SQLiteException

when the database is closed or a thread confined connection needs to be upgraded or there is a timeout acquiring a connection.

useWriterConnection

public static final @NonNull R <R extends Object> useWriterConnection(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block
)

Acquires a WRITE connection, suspending while waiting if none is available and then calling the block to use the connection once it is acquired. A RoomDatabase will have only one WRITE connection. The connection to use in the block is an instance of Transactor that provides the capabilities for performing nested transactions.

Using the connection after block completes is prohibited.

The connection will be confined to the coroutine on which block executes, attempting to use the connection from a different coroutine will result in an error.

If the current coroutine calling this function already has a confined connection, then that connection is used as long as it isn't required to be upgraded to a writer. If an upgrade is required then a SQLiteException is thrown.

A connection is a limited resource and should not be held for more than it is needed. The best practice in using connections is to avoid executing long-running computations within the block. If a caller has to wait too long to acquire a connection a SQLiteException will be thrown due to a timeout.

Parameters
@NonNull SuspendFunction1<@NonNull Transactor, @NonNull R> block

The code to use the connection.

Throws
SQLiteException

when the database is closed or a thread confined connection needs to be upgraded or there is a timeout acquiring a connection.

withReadTransaction

public static final @NonNull R <R extends Object> withReadTransaction(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull TransactionScope<@NonNull R>, @NonNull R> block
)

Acquire a READ connection and start a Transactor.SQLiteTransactionType.DEFERRED transaction to execute the given block within the transaction.

This function is a shorthand of:

roomDatabase.useReaderConnection { it.withTransaction(DEFERRED) { block() } }

withWriteTransaction

public static final @NonNull R <R extends Object> withWriteTransaction(
    @NonNull RoomDatabase receiver,
    @NonNull SuspendFunction1<@NonNull TransactionScope<@NonNull R>, @NonNull R> block
)

Acquire a WRITE connection and start a Transactor.SQLiteTransactionType.IMMEDIATE transaction to execute the given block within the transaction.

This function is a shorthand of:

roomDatabase.useWriterConnection { it.withTransaction(IMMEDIATE) { block() } }