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 |
public abstract class RoomDatabase.CallbackCallback for |
public enum RoomDatabase.JournalMode extends EnumJournal modes for SQLite database. |
public final class RoomDatabase.MigrationContainerA container to hold migrations. |
public abstract class RoomDatabase.PrepackagedDatabaseCallbackCallback for |
public fun interface RoomDatabase.QueryCallbackCallback interface for when SQLite queries are executed. |
Protected fields |
|
|---|---|
volatile final SupportSQLiteDatabase |
This field is deprecated. This property is always null and will be removed in a future version. |
Public constructors |
|---|
|
You cannot create an instance of a database, instead, you should acquire it via #Room.databaseBuilder or #Room.inMemoryDatabaseBuilder. |
Public methods |
|
|---|---|
void |
This method is deprecated. beginTransaction() is deprecated |
abstract void |
Deletes all rows from all the tables that are registered to this database as |
void |
close()Closes the database. |
@NonNull SupportSQLiteStatement |
compileStatement(@NonNull String sql)Wrapper for |
void |
This method is deprecated. endTransaction() is deprecated |
@NonNull InvalidationTracker |
The invalidation tracker for this database. |
@NonNull SupportSQLiteOpenHelper |
The SQLite open helper used by this database. |
@NonNull Executor |
The Executor in use by this database for async queries. |
@NonNull Executor |
The Executor in use by this database for async transactions. |
T |
<T extends Object> This method is deprecated. No longer called by generated implementation |
boolean |
Wrapper for |
void |
@CallSuperCalled by Room when it is initialized. |
boolean |
isOpen()True if database connection is open and initialized. |
@NonNull Cursor |
Convenience method to query the database with arguments. |
@NonNull Cursor |
query(@NonNull SupportSQLiteQuery query, CancellationSignal signal)Wrapper for |
@NonNull V |
<V extends Object> runInTransaction(@NonNull Callable<@NonNull V> body)Executes the specified |
void |
runInTransaction(@NonNull Runnable body)Executes the specified |
void |
This method is deprecated. setTransactionSuccessful() is deprecated |
Protected methods |
|
|---|---|
abstract @NonNull InvalidationTracker |
Creates the invalidation tracker |
@NonNull SupportSQLiteOpenHelper |
This method is deprecated. No longer implemented by generated |
void |
This method is deprecated. No longer called by generated |
Extension functions |
|
|---|---|
final @NonNull Flow<@NonNull Set<@NonNull String>> |
RoomDatabaseKt.This method is deprecated. Replaced by equivalent API in InvalidationTracker. |
final @NonNull <Error class: unknown class> |
RoomSupportSQLite.getSupportWrapper(@NonNull RoomDatabase receiver)Gets a new SupportSQLiteDatabase implementation that is backed by a |
Protected fields
Public constructors
RoomDatabase
public RoomDatabase()
You cannot create an instance of a database, instead, you should acquire it via #Room.databaseBuilder or #Room.inMemoryDatabaseBuilder.
Public methods
public void beginTransaction()Wrapper for SupportSQLiteDatabase.beginTransaction.
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
clearAllTables
@WorkerThread
public abstract void clearAllTables()
Deletes all rows from all the tables that are registered to this database as Database.entities.
This does NOT reset the auto-increment value generated by PrimaryKey.autoGenerate.
After deleting the rows, Room will set a WAL checkpoint and run VACUUM. This means that the data is completely erased. The space will be reclaimed by the system if the amount surpasses the threshold of database file size.
See SQLite documentation for details. FileFormat
close
public void close()
Closes the database.
Once a RoomDatabase is closed it should no longer be used.
compileStatement
public @NonNull SupportSQLiteStatement compileStatement(@NonNull String sql)
Wrapper for SupportSQLiteDatabase.compileStatement.
| Returns | |
|---|---|
@NonNull SupportSQLiteStatement |
The compiled query. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
public void endTransaction()Wrapper for SupportSQLiteDatabase.endTransaction.
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
getInvalidationTracker
public @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. |
getOpenHelper
public @NonNull SupportSQLiteOpenHelper getOpenHelper()
The SQLite open helper used by this database.
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
getQueryExecutor
public @NonNull Executor getQueryExecutor()
The Executor in use by this database for async queries.
getTransactionExecutor
public @NonNull Executor getTransactionExecutor()
The Executor in use by this database for async transactions.
public T <T extends Object>getTypeConverter(@NonNull Class<@NonNull T> klass)
Gets the instance of the given Type Converter.
| Parameters | |
|---|---|
<T extends Object> |
The type of the expected Type Converter subclass. |
@NonNull Class<@NonNull T> klass |
The Type Converter class. |
| Returns | |
|---|---|
T |
An instance of T if it is provided in the builder. |
inTransaction
public boolean inTransaction()
Wrapper for SupportSQLiteDatabase.inTransaction. Returns true if current thread is in a transaction.
| Returns | |
|---|---|
boolean |
True if there is an active transaction in current thread, false otherwise. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
| See also | |
|---|---|
inTransaction |
init
@CallSuper
public void init(@NonNull DatabaseConfiguration configuration)
Called by Room when it is initialized.
| Parameters | |
|---|---|
@NonNull DatabaseConfiguration configuration |
The database configuration. |
| Throws | |
|---|---|
kotlin.IllegalArgumentException |
if initialization fails. |
isOpen
public boolean isOpen()
True if database connection is open and initialized.
When Room is configured with RoomDatabase.Builder.setAutoCloseTimeout the database is considered open even if internally the connection has been closed, unless manually closed.
| Returns | |
|---|---|
boolean |
true if the database connection is open, false otherwise. |
query
public @NonNull Cursor query(@NonNull String query, Object[] args)
Convenience method to query the database with arguments.
| Parameters | |
|---|---|
@NonNull String query |
The sql query |
Object[] args |
The bind arguments for the placeholders in the query |
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
query
public @NonNull Cursor query(@NonNull SupportSQLiteQuery query, CancellationSignal signal)
Wrapper for SupportSQLiteDatabase.query.
| Parameters | |
|---|---|
@NonNull SupportSQLiteQuery query |
The Query which includes the SQL and a bind callback for bind arguments. |
CancellationSignal signal |
The cancellation signal to be attached to the query. |
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
runInTransaction
public @NonNull V <V extends Object> runInTransaction(@NonNull Callable<@NonNull V> body)
Executes the specified Callable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Callable.
Room will only perform at most one transaction at a time.
If a SQLiteDriver is configured with this database, then it is best to use useWriterConnection along with immediateTransaction to perform transactional operations.
runInTransaction
public void runInTransaction(@NonNull Runnable body)
Executes the specified Runnable in a database transaction. The transaction will be marked as successful unless an exception is thrown in the Runnable.
Room will only perform at most one transaction at a time.
If a SQLiteDriver is configured with this database, then it is best to use useWriterConnection along with immediateTransaction to perform transactional operations.
public void setTransactionSuccessful()Wrapper for SupportSQLiteDatabase.setTransactionSuccessful.
| Throws | |
|---|---|
kotlin.IllegalStateException |
If a |
Protected methods
createInvalidationTracker
protected abstract @NonNull InvalidationTracker createInvalidationTracker()
Creates the invalidation tracker
An implementation of this function is generated by the Room processor. Note that this function is called when the RoomDatabase is initialized.
| Returns | |
|---|---|
@NonNull InvalidationTracker |
A new invalidation tracker. |
protected @NonNull SupportSQLiteOpenHelpercreateOpenHelper(@NonNull DatabaseConfiguration config)
Creates the open helper to access the database. Generated class already implements this method. Note that this method is called when the RoomDatabase is initialized.
| Parameters | |
|---|---|
@NonNull DatabaseConfiguration config |
The configuration of the Room database. |
| Returns | |
|---|---|
@NonNull SupportSQLiteOpenHelper |
A new SupportSQLiteOpenHelper to be used while connecting to the database. |
| Throws | |
|---|---|
kotlin.NotImplementedError |
by default |
protected voidinternalInitInvalidationTracker(@NonNull SupportSQLiteDatabase db)
Initialize invalidation tracker. Note that this method is called when the RoomDatabase is initialized and opens a database connection.
| Parameters | |
|---|---|
@NonNull SupportSQLiteDatabase db |
The database instance. |
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)
}
}RoomSupportSQLite.getSupportWrapper
public final @NonNull <Error class: unknown class> RoomSupportSQLite.getSupportWrapper(@NonNull RoomDatabase receiver)
Gets a new SupportSQLiteDatabase implementation that is backed by a RoomDatabase for compatibility with existing usages of SupportSQLiteDatabase.