RxDaoReturnTypeConverters


public final 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 methods

final @NonNull Completable
@DaoReturnTypeConverter(operations = [OperationType.WRITE])
convertCompletable(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<Unit> executeAndConvert
)

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

final @NonNull Flowable<@NonNull T>
@DaoReturnTypeConverter(operations = [OperationType.READ])
<T extends Object> convertFlowable(
    @NonNull RoomDatabase database,
    @NonNull String[] tableNames,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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

final @NonNull Maybe<@NonNull T>
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
<T extends Object> convertMaybe(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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

final @NonNull Observable<@NonNull T>
@DaoReturnTypeConverter(operations = [OperationType.READ])
<T extends Object> convertObservable(
    @NonNull RoomDatabase database,
    @NonNull String[] tableNames,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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

final @NonNull Single<@NonNull T>
@DaoReturnTypeConverter(operations = [OperationType.READOperationType.WRITE])
<T extends Object> convertSingle(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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
public RxDaoReturnTypeConverters()

Public methods

convertCompletable

Added in 3.0.0-alpha02
@DaoReturnTypeConverter(operations = [OperationType.WRITE])
public final @NonNull Completable convertCompletable(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<Unit> executeAndConvert
)

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
@NonNull RoomDatabase database

RoomDatabase instance

@NonNull SuspendFunction0<Unit> executeAndConvert

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])
public final @NonNull Flowable<@NonNull T> <T extends Object> convertFlowable(
    @NonNull RoomDatabase database,
    @NonNull String[] tableNames,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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
@NonNull RoomDatabase database

RoomDatabase instance

@NonNull String[] tableNames

List of names of the tables of the RoomDatabase

@NonNull SuspendFunction0<T> executeAndConvert

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])
public final @NonNull Maybe<@NonNull T> <T extends Object> convertMaybe(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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
@NonNull RoomDatabase database

RoomDatabase instance

@NonNull SuspendFunction0<T> executeAndConvert

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])
public final @NonNull Observable<@NonNull T> <T extends Object> convertObservable(
    @NonNull RoomDatabase database,
    @NonNull String[] tableNames,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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
@NonNull RoomDatabase database

RoomDatabase instance

@NonNull String[] tableNames

List of names of the tables of the RoomDatabase

@NonNull SuspendFunction0<T> executeAndConvert

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])
public final @NonNull Single<@NonNull T> <T extends Object> convertSingle(
    @NonNull RoomDatabase database,
    @NonNull SuspendFunction0<T> executeAndConvert
)

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
@NonNull RoomDatabase database

RoomDatabase instance

@NonNull SuspendFunction0<T> executeAndConvert

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