Builder

open class Builder<T : RoomDatabase!>
kotlin.Any
   ↳ androidx.room.RoomDatabase.Builder

Builder for RoomDatabase.

Summary

Public methods
open RoomDatabase.Builder<T>

Disables the main thread query check for Room.

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

Adds a migration to the builder.

open RoomDatabase.Builder<T>

Adds a Callback to this database.

open 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.

open RoomDatabase.Builder<T>
openHelperFactory(factory: Factory?)

Sets the database factory.

open RoomDatabase.Builder<T>

Sets the journal mode for this database.

open RoomDatabase.Builder<T>
fallbackToDestructiveMigrationFrom(vararg startVersions: Int)

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

open T

Creates the databases and initializes it.

Public methods

allowMainThreadQueries

open fun allowMainThreadQueries(): RoomDatabase.Builder<T>

Disables the main thread query check for Room.

Room ensures that Database is never accessed on the main thread because it may lock the main thread and trigger an ANR. If you need to access the database from the main thread, you should always use async alternatives or manually move the call to a background thread.

You may want to turn this check off for testing.

Return
RoomDatabase.Builder<T>: this

addMigrations

open 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.

If a migration item is missing between current version and the latest version, Room will clear the database and recreate so even if you have no changes between 2 versions, you should still provide a Migration object to the builder.

A migration can handle more than 1 version (e.g. if you have a faster path to choose when going 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
migrations Migration!: The migration object that can modify the database and to the necessary changes.
Return
RoomDatabase.Builder<T>: this

addCallback

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

Adds a Callback to this database.

Parameters
callback RoomDatabase.Callback: The callback.
Return
RoomDatabase.Builder<T>: this

fallbackToDestructiveMigration

open fun fallbackToDestructiveMigration(): 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 instead of crashing.

Note that this will delete all of the data in the database tables managed by Room.

Return
RoomDatabase.Builder<T>: this

openHelperFactory

open fun openHelperFactory(factory: Factory?): RoomDatabase.Builder<T>

Sets the database factory. If not set, it defaults to FrameworkSQLiteOpenHelperFactory.

Parameters
factory Factory?: The factory to use to access the database.
Return
RoomDatabase.Builder<T>: this

setJournalMode

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

Sets the journal mode for this database.

This value is ignored if the builder is initialized with Room#inMemoryDatabaseBuilder(Context, Class).

The journal mode should be consistent across multiple instances of RoomDatabase for a single SQLite database file.

The default value is JournalMode#AUTOMATIC.

Parameters
journalMode RoomDatabase.JournalMode: The journal mode.
Return
RoomDatabase.Builder<T>: this

fallbackToDestructiveMigrationFrom

open fun fallbackToDestructiveMigrationFrom(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 as that provided by #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 to #addMigrations(Migration...). If a version passed to this method is found as a starting or ending version in a Migration, an exception will be thrown.

Parameters
startVersions Int: The set of schema versions from which Room should use a destructive migration.
Return
RoomDatabase.Builder<T>: this

build

open fun build(): T

Creates the databases and initializes it.

By default, all RoomDatabases use in memory storage for TEMP tables and enables recursive triggers.

Return
T: A new database instance.