Google is committed to advancing racial equity for Black communities. See how.

Builder

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

Builder for RoomDatabase.

Summary

Public methods
open RoomDatabase.Builder<T>
addCallback(@NonNull callback: RoomDatabase.Callback)

Adds a Callback to this database.

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

Adds a migration to the builder.

open RoomDatabase.Builder<T>
addTypeConverter(@NonNull typeConverter: Any)

Adds a type converter instance to this database.

open RoomDatabase.Builder<T>

Disables the main thread query check for Room.

open T

Creates the databases and initializes it.

open RoomDatabase.Builder<T>
createFromAsset(@NonNull databaseFilePath: String)

Configures Room to create and open the database using a pre-packaged database located in the application 'assets/' folder.

open RoomDatabase.Builder<T>
createFromAsset(@NonNull databaseFilePath: String, @NonNull callback: RoomDatabase.PrepackagedDatabaseCallback)

Configures Room to create and open the database using a pre-packaged database located in the application 'assets/' folder.

open RoomDatabase.Builder<T>
createFromFile(@NonNull databaseFile: File)

Configures Room to create and open the database using a pre-packaged database file.

open RoomDatabase.Builder<T>
createFromFile(@NonNull databaseFile: File, @NonNull callback: RoomDatabase.PrepackagedDatabaseCallback)

Configures Room to create and open the database using a pre-packaged database file.

open RoomDatabase.Builder<T>
createFromInputStream(@NonNull inputStreamCallable: Callable<InputStream!>)

Configures Room to create and open the database using a pre-packaged database via an InputStream.

open RoomDatabase.Builder<T>
createFromInputStream(@NonNull inputStreamCallable: Callable<InputStream!>, @NonNull callback: RoomDatabase.PrepackagedDatabaseCallback)

Configures Room to create and open the database using a pre-packaged database via an InputStream.

open RoomDatabase.Builder<T>

Sets whether table invalidation in this instance of RoomDatabase should be broadcast and synchronized with other instances of the same RoomDatabase, including those in a separate process.

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>
fallbackToDestructiveMigrationFrom(vararg startVersions: Int)

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

open RoomDatabase.Builder<T>

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

open RoomDatabase.Builder<T>

Sets the database factory.

open RoomDatabase.Builder<T>

Sets the journal mode for this database.

open RoomDatabase.Builder<T>
setQueryCallback(@NonNull queryCallback: RoomDatabase.QueryCallback, @NonNull executor: Executor)

Sets a QueryCallback to be invoked when queries are executed.

open RoomDatabase.Builder<T>
setQueryExecutor(@NonNull executor: Executor)

Sets the Executor that will be used to execute all non-blocking asynchronous queries and tasks, including LiveData invalidation, Flowable scheduling and ListenableFuture tasks.

open RoomDatabase.Builder<T>
setTransactionExecutor(@NonNull executor: Executor)

Sets the Executor that will be used to execute all non-blocking asynchronous transaction queries and tasks, including LiveData invalidation, Flowable scheduling and ListenableFuture tasks.

Public methods

addCallback

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

Adds a Callback to this database.

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

addMigrations

@NonNull open fun addMigrations(@NonNull 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 Builder instance.

addTypeConverter

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

Adds a type converter instance to this database.

Parameters
typeConverter Any: The converter. It must be an instance of a class annotated with ProvidedTypeConverter otherwise Room will throw an exception.
Return
RoomDatabase.Builder<T> This Builder instance.

allowMainThreadQueries

@NonNull 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 Builder instance.

build

@NonNull 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.

createFromAsset

@NonNull open fun createFromAsset(@NonNull databaseFilePath: String): RoomDatabase.Builder<T>

Configures Room to create and open the database using a pre-packaged database located in the application 'assets/' folder.

Room does not open the pre-packaged database, instead it copies it into the internal app database folder and then opens it. The pre-packaged database file must be located in the "assets/" folder of your application. For example, the path for a file located in "assets/databases/products.db" would be "databases/products.db".

The pre-packaged database schema will be validated. It might be best to create your pre-packaged database schema utilizing the exported schema files generated when Database#exportSchema() is enabled.

This method is not supported for an in memory database Builder.

Parameters
databaseFilePath String: The file path within the 'assets/' directory of where the database file is located.
Return
RoomDatabase.Builder<T> This Builder instance.

createFromAsset

@NonNull open fun createFromAsset(
    @NonNull databaseFilePath: String,
    @NonNull callback: RoomDatabase.PrepackagedDatabaseCallback
): RoomDatabase.Builder<T>

Configures Room to create and open the database using a pre-packaged database located in the application 'assets/' folder.

Room does not open the pre-packaged database, instead it copies it into the internal app database folder and then opens it. The pre-packaged database file must be located in the "assets/" folder of your application. For example, the path for a file located in "assets/databases/products.db" would be "databases/products.db".

The pre-packaged database schema will be validated. It might be best to create your pre-packaged database schema utilizing the exported schema files generated when Database#exportSchema() is enabled.

This method is not supported for an in memory database Builder.

Parameters
databaseFilePath String: The file path within the 'assets/' directory of where the database file is located.
callback RoomDatabase.PrepackagedDatabaseCallback: The pre-packaged callback.
Return
RoomDatabase.Builder<T> This Builder instance.

createFromFile

@NonNull open fun createFromFile(@NonNull databaseFile: File): RoomDatabase.Builder<T>

Configures Room to create and open the database using a pre-packaged database file.

Room does not open the pre-packaged database, instead it copies it into the internal app database folder and then opens it. The given file must be accessible and the right permissions must be granted for Room to copy the file.

The pre-packaged database schema will be validated. It might be best to create your pre-packaged database schema utilizing the exported schema files generated when Database#exportSchema() is enabled.

The Callback#onOpen(SupportSQLiteDatabase) method can be used as an indicator that the pre-packaged database was successfully opened by Room and can be cleaned up.

This method is not supported for an in memory database Builder.

Parameters
databaseFile File: The database file.
Return
RoomDatabase.Builder<T> This Builder instance.

createFromFile

@NonNull open fun createFromFile(
    @NonNull databaseFile: File,
    @NonNull callback: RoomDatabase.PrepackagedDatabaseCallback
): RoomDatabase.Builder<T>

Configures Room to create and open the database using a pre-packaged database file.

Room does not open the pre-packaged database, instead it copies it into the internal app database folder and then opens it. The given file must be accessible and the right permissions must be granted for Room to copy the file.

The pre-packaged database schema will be validated. It might be best to create your pre-packaged database schema utilizing the exported schema files generated when Database#exportSchema() is enabled.

The Callback#onOpen(SupportSQLiteDatabase) method can be used as an indicator that the pre-packaged database was successfully opened by Room and can be cleaned up.

This method is not supported for an in memory database Builder.

Parameters
databaseFile