RoomDatabase.Builder



Builder for RoomDatabase.

Parameters
<T : RoomDatabase>

The type of the abstract database class.

Summary

Public functions

RoomDatabase.Builder<T>

Adds an auto migration spec instance to the builder.

Cmn
N
RoomDatabase.Builder<T>

Adds a Callback to this database.

Cmn
N
RoomDatabase.Builder<T>
addMigrations(vararg migrations: Migration)

Adds a migration to the builder.

Cmn
N
RoomDatabase.Builder<T>
addTypeConverter(typeConverter: Any)

Adds a type converter instance to the builder.

Cmn
N
T

Creates the database and initializes it.

Cmn
N
RoomDatabase.Builder<T>

Allows Room to destructively recreate database tables if Migrations that would migrate old database schemas to the latest schema version are not found.

Cmn
N
RoomDatabase.Builder<T>
fallbackToDestructiveMigrationFrom(
    dropAllTables: Boolean,
    vararg startVersions: Int
)

Informs Room that it is allowed to destructively recreate database tables from specific starting schema versions.

Cmn
N
RoomDatabase.Builder<T>

Allows Room to destructively recreate database tables if Migrations are not available when downgrading to old schema versions.

Cmn
N
RoomDatabase.Builder<T>

Sets the SQLiteDriver implementation to be used by Room to open database connections.

Cmn
N
RoomDatabase.Builder<T>

Sets the journal mode for this database.

Cmn
N
RoomDatabase.Builder<T>

Sets the CoroutineContext that will be used to execute all asynchronous queries and tasks, such as Flow emissions and InvalidationTracker notifications.

Cmn

Public functions

addAutoMigrationSpec

fun addAutoMigrationSpec(autoMigrationSpec: AutoMigrationSpec): RoomDatabase.Builder<T>

Adds an auto migration spec instance to the builder.

Parameters
autoMigrationSpec: AutoMigrationSpec

The auto migration object that is annotated with ProvidedAutoMigrationSpec and is declared in an AutoMigration annotation.

Returns
RoomDatabase.Builder<T>

This builder instance.

addCallback

fun addCallback(callback: RoomDatabase.Callback): RoomDatabase.Builder<T>

Adds a Callback to this database.

Parameters
callback: RoomDatabase.Callback

The callback.

Returns
RoomDatabase.Builder<T>

This builder instance.

addMigrations

fun addMigrations(vararg migrations: Migration): RoomDatabase.Builder<T>

Adds a migration to the builder.

Each Migration has a start and end versions and Room runs these migrations to bring the database to the latest version.

A migration can handle more than 1 version (e.g. if you have a faster path to choose when going from version 3 to 5 without going to version 4). If Room opens a database at version 3 and latest version is >= 5, Room will use the migration object that can migrate from 3 to 5 instead of 3 to 4 and 4 to 5.

Parameters
vararg migrations: Migration

The migration objects that modify the database schema with the necessary changes for a version change.

Returns
RoomDatabase.Builder<T>

This builder instance.

addTypeConverter

fun addTypeConverter(typeConverter: Any): RoomDatabase.Builder<T>

Adds a type converter instance to the builder.

Parameters
typeConverter: Any

The converter instance that is annotated with ProvidedTypeConverter.

Returns
RoomDatabase.Builder<T>

This builder instance.

build

fun build(): T

Creates the database and initializes it.

Returns
T

A new database instance.

Throws
kotlin.IllegalArgumentException

if the builder was misconfigured.

fallbackToDestructiveMigration

fun fallbackToDestructiveMigration(dropAllTables: Boolean): RoomDatabase.Builder<T>

Allows Room to destructively recreate database tables if Migrations that would migrate old database schemas to the latest schema version are not found.

When the database version on the device does not match the latest schema version, Room runs necessary Migrations on the database. If it cannot find the set of Migrations that will bring the database to the current version, it will throw an IllegalStateException. You can call this method to change this behavior to re-create the database tables instead of crashing.

To let Room fallback to destructive migration only during a schema downgrade then use fallbackToDestructiveMigrationOnDowngrade.

Parameters
dropAllTables: Boolean

Set to true if all tables should be dropped during destructive migration including those not managed by Room. Recommended value is true as otherwise Room could leave obsolete data when table names or existence changes between versions.

Returns
RoomDatabase.Builder<T>

This builder instance.

fallbackToDestructiveMigrationFrom

fun fallbackToDestructiveMigrationFrom(
    dropAllTables: Boolean,
    vararg startVersions: Int
): RoomDatabase.Builder<T>

Informs Room that it is allowed to destructively recreate database tables from specific starting schema versions.

This functionality is the same fallbackToDestructiveMigration, except that this method allows the specification of a set of schema versions for which destructive recreation is allowed.

Using this method is preferable to fallbackToDestructiveMigration if you want to allow destructive migrations from some schema versions while still taking advantage of exceptions being thrown due to unintentionally missing migrations.

Note: No versions passed to this method may also exist as either starting or ending versions in the Migrations provided via addMigrations. If a version passed to this method is found as a starting or ending version in a Migration, an exception will be thrown.

Parameters
dropAllTables: Boolean

Set to true if all tables should be dropped during destructive migration including those not managed by Room. Recommended value is true as otherwise Room could leave obsolete data when table names or existence changes between versions.

vararg startVersions: Int

The set of schema versions from which Room should use a destructive migration.

Returns
RoomDatabase.Builder<T>

This builder instance.

fallbackToDestructiveMigrationOnDowngrade

fun fallbackToDestructiveMigrationOnDowngrade(dropAllTables: Boolean): RoomDatabase.Builder<T>

Allows Room to destructively recreate database tables if Migrations are not available when downgrading to old schema versions.

For details, see Builder.fallbackToDestructiveMigration.

Parameters
dropAllTables: Boolean

Set to true if all tables should be dropped during destructive migration including those not managed by Room. Recommended value is true as otherwise Room could leave obsolete data when table names or existence changes between versions.

Returns
RoomDatabase.Builder<T>

This builder instance.

setDriver

fun setDriver(driver: SQLiteDriver): RoomDatabase.Builder<T>

Sets the SQLiteDriver implementation to be used by Room to open database connections.

Parameters
driver: SQLiteDriver

The driver

Returns
RoomDatabase.Builder<T>

This builder instance.

setJournalMode

fun setJournalMode(journalMode: RoomDatabase.JournalMode): RoomDatabase.Builder<T>

Sets the journal mode for this database.

The value is ignored if the builder is for an 'in-memory database'. The journal mode should be consistent across multiple instances of RoomDatabase for a single SQLite database file.

The default value is JournalMode.WRITE_AHEAD_LOGGING.

Parameters
journalMode: RoomDatabase.JournalMode

The journal mode.

Returns
RoomDatabase.Builder<T>

This builder instance.

setQueryCoroutineContext

fun setQueryCoroutineContext(context: CoroutineContext): RoomDatabase.Builder<T>

Sets the CoroutineContext that will be used to execute all asynchronous queries and tasks, such as Flow emissions and InvalidationTracker notifications.

If no CoroutineDispatcher is present in the context then this function will throw an IllegalArgumentException

Parameters
context: CoroutineContext

The context

Returns
RoomDatabase.Builder<T>

This Builder instance