RxDaoReturnTypeConverters


class RxDaoReturnTypeConverters


A DaoReturnTypeConverter that allows Room to return RxJava3 types from @Dao functions.

When defining a converter for a reactive type that supports null values or empty states (e.g. Maybe), the function type parameter must be restricted to <T : Any>, and the executeAndConvert lambda must return T?, signaling Room to handle a null/empty result when converting this DAO return type.

You can register this converter via annotating a androidx.room3.Database or androidx.room3.Dao using the annotation androidx.room3.DaoReturnTypeConverters:

@DaoReturnTypeConverters(
Rx3DaoReturnTypeConverters::class
)

Summary

Public constructors

Public functions

Completable
@DaoReturnTypeConverter(operations = [OperationType.WRITE])
convertCompletable(
    database: RoomDatabase,
    executeAndConvert: suspend () -> Unit?
)

This convertCompletable function will be called from Room generated code to convert a Room query result to the return type of this function.

Flowable<T>
@DaoReturnTypeConverter(operations = [OperationType.READ])
<T : Any> convertFlowable(
    database: RoomDatabase,
    tableNames: Array<String>,
    executeAndConvert: suspend () -> T?
)

This convertFlowable function will be called from Room generated code to convert a Room query result to the return type of this function.

Maybe<T>
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
<T : Any> convertMaybe(
    database: RoomDatabase,
    executeAndConvert: suspend () -> T?
)

This convertMaybe function will be called from Room generated code to convert a Room query result to the return type of this function.

Observable<T>
@DaoReturnTypeConverter(operations = [OperationType.READ])
<T : Any> convertObservable(
    database: RoomDatabase,
    tableNames: Array<String>,
    executeAndConvert: suspend () -> T?
)

This convertObservable function will be called from Room generated code to convert a Room query result to the return type of this function.

Single<T>
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
<T : Any> convertSingle(
    database: RoomDatabase,
    executeAndConvert: suspend () -> T?
)

This convertSingle function will be called from Room generated code to convert a Room query result to the return type of this function.

Public constructors

RxDaoReturnTypeConverters

Added in 3.0.0-alpha02
RxDaoReturnTypeConverters()

Public functions

convertCompletable

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.WRITE])
fun convertCompletable(
    database: RoomDatabase,
    executeAndConvert: suspend () -> Unit?
): Completable

This convertCompletable function will be called from Room generated code to convert a Room query result to the return type of this function.

This converter is restricted to OperationType.WRITE via the DaoReturnTypeConverter.operations property, as Completable is typically used for operations that modify the database without returning a value, such as Room shortcut methods (@Insert, @Update, @Delete).

Parameters
database: RoomDatabase

RoomDatabase instance

executeAndConvert: suspend () -> Unit?

A suspend lambda function that invokes the part of the generated code that executes the query.

convertFlowable

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.READ])
fun <T : Any> convertFlowable(
    database: RoomDatabase,
    tableNames: Array<String>,
    executeAndConvert: suspend () -> T?
): Flowable<T>

This convertFlowable function will be called from Room generated code to convert a Room query result to the return type of this function.

This converter is restricted to OperationType.READ via the DaoReturnTypeConverter.operations property, as Flowable is intended for observing continuous data changes.

Parameters
database: RoomDatabase

RoomDatabase instance

tableNames: Array<String>

List of names of the tables of the RoomDatabase

executeAndConvert: suspend () -> T?

A suspend lambda function that invokes the part of the generated code that executes the query.

convertMaybe

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
fun <T : Any> convertMaybe(
    database: RoomDatabase,
    executeAndConvert: suspend () -> T?
): Maybe<T>

This convertMaybe function will be called from Room generated code to convert a Room query result to the return type of this function.

This converter can be used for both OperationType.READ and OperationType.WRITE. Note that Room shortcut methods (@Insert, @Update, @Delete) are always treated as OperationType.WRITE.

Parameters
database: RoomDatabase

RoomDatabase instance

executeAndConvert: suspend () -> T?

A suspend lambda function that invokes the part of the generated code that executes the query.

convertObservable

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.READ])
fun <T : Any> convertObservable(
    database: RoomDatabase,
    tableNames: Array<String>,
    executeAndConvert: suspend () -> T?
): Observable<T>

This convertObservable function will be called from Room generated code to convert a Room query result to the return type of this function.

This converter is restricted to OperationType.READ via the DaoReturnTypeConverter.operations property, as Flowable is intended for observing continuous data changes.

Parameters
database: RoomDatabase

RoomDatabase instance

tableNames: Array<String>

List of names of the tables of the RoomDatabase

executeAndConvert: suspend () -> T?

A suspend lambda function that invokes the part of the generated code that executes the query.

convertSingle

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
fun <T : Any> convertSingle(
    database: RoomDatabase,
    executeAndConvert: suspend () -> T?
): Single<T>

This convertSingle function will be called from Room generated code to convert a Room query result to the return type of this function.

This converter can be used for both OperationType.READ and OperationType.WRITE. Note that Room shortcut methods (@Insert, @Update, @Delete) are always treated as OperationType.WRITE.

Parameters
database: RoomDatabase

RoomDatabase instance

executeAndConvert: suspend () -> T?

A suspend lambda function that invokes the part of the generated code that executes the query.