RoomDatabase


public abstract class RoomDatabase


Base class for all Room databases. All classes that are annotated with Database must extend this class.

RoomDatabase provides direct access to the underlying database implementation but you should prefer using Dao classes.

See also
Database

Summary

Nested types

public final class RoomDatabase.Builder<T extends RoomDatabase>

Builder for RoomDatabase.

public abstract class RoomDatabase.Callback

Callback for RoomDatabase

public enum RoomDatabase.JournalMode extends Enum

Journal modes for SQLite database.

A container to hold migrations.

Public constructors

Public methods

final void

Closes the database.

final @NonNull InvalidationTracker

The invalidation tracker for this database.

Protected methods

abstract @NonNull InvalidationTracker

Creates the invalidation tracker

Extension functions

final @NonNull Flow<@NonNull Set<@NonNull String>>
RoomDatabaseKt.invalidationTrackerFlow(
    @NonNull RoomDatabase receiver,
    @NonNull String tables,
    boolean emitInitialState
)

This method is deprecated. Replaced by equivalent API in InvalidationTracker.

Public constructors

RoomDatabase

Added in 2.0.0
public RoomDatabase()

Public methods

close

Added in 2.0.0
public final void close()

Closes the database.

Once a RoomDatabase is closed it should no longer be used.

getInvalidationTracker

Added in 2.0.0
public final @NonNull InvalidationTracker getInvalidationTracker()

The invalidation tracker for this database.

You can use the invalidation tracker to get notified when certain tables in the database are modified.

Returns
@NonNull InvalidationTracker

The invalidation tracker for the database.

Protected methods

createInvalidationTracker

Added in 2.0.0
protected abstract @NonNull InvalidationTracker createInvalidationTracker()

Creates the invalidation tracker

An implementation of this function is generated by the Room processor. Note that this method is called when the RoomDatabase is initialized.

Returns
@NonNull InvalidationTracker

A new invalidation tracker.

Extension functions

RoomDatabaseKt.invalidationTrackerFlow

public final @NonNull Flow<@NonNull Set<@NonNull String>> RoomDatabaseKt.invalidationTrackerFlow(
    @NonNull RoomDatabase receiver,
    @NonNull String tables,
    boolean emitInitialState
)

Creates a Flow that listens for changes in the database via the InvalidationTracker and emits sets of the tables that were invalidated.

The Flow will emit at least one value, a set of all the tables registered for observation to kick-start the stream unless emitInitialState is set to false.

If one of the tables to observe does not exist in the database, this functions throws an IllegalArgumentException.

The returned Flow can be used to create a stream that reacts to changes in the database:

fun getArtistTours(from: Date, to: Date): Flow<Map<Artist, TourState>> {
return db.invalidationTrackerFlow("Artist").map { _ ->
val artists = artistsDao.getAllArtists()
val tours = tourService.fetchStates(artists.map { it.id })
associateTours(artists, tours, from, to)
}
}
Parameters
@NonNull String tables

The name of the tables or views to observe.

boolean emitInitialState

Set to false if no initial emission is desired. Default value is true.