Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.persistence.room:runtime:1.0.0

RoomDatabase.Builder

public static class RoomDatabase.Builder
extends Object

java.lang.Object
   ↳ android.arch.persistence.room.RoomDatabase.Builder<T extends android.arch.persistence.room.RoomDatabase>


Builder for RoomDatabase.

Summary

Public methods

Builder<T> addCallback(RoomDatabase.Callback callback)

Adds a RoomDatabase.Callback to this database.

Builder<T> addMigrations(Migration... migrations)

Adds a migration to the builder.

Builder<T> allowMainThreadQueries()

Disables the main thread query check for Room.

T build()

Creates the databases and initializes it.

Builder<T> fallbackToDestructiveMigration()

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

Builder<T> openHelperFactory(SupportSQLiteOpenHelper.Factory factory)

Sets the database factory.

Inherited methods

From class java.lang.Object

Public methods

addCallback

added in version 1.0.0
Builder<T> addCallback (RoomDatabase.Callback callback)

Adds a RoomDatabase.Callback to this database.

Parameters
callback RoomDatabase.Callback: The callback.

Returns
Builder<T> this

addMigrations

added in version 1.0.0
Builder<T> addMigrations (Migration... migrations)

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.

Returns
Builder<T> this

allowMainThreadQueries

added in version 1.0.0
Builder<T> allowMainThreadQueries ()

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.

Returns
Builder<T> this

build

added in version 1.0.0
T build ()

Creates the databases and initializes it.

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

Returns
T A new database instance.

fallbackToDestructiveMigration

added in version 1.0.0
Builder<T> fallbackToDestructiveMigration ()

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.

Returns
Builder<T> this

openHelperFactory

added in version 1.0.0
Builder<T> openHelperFactory (SupportSQLiteOpenHelper.Factory factory)

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

Parameters
factory SupportSQLiteOpenHelper.Factory: The factory to use to access the database.

Returns
Builder<T> this

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience. (Dec 2017 Android Platform & Tools Survey)