InvalidationTracker


public final class InvalidationTracker


The invalidation tracker keeps track of tables modified by queries and notifies its created Flows about such modifications.

A Flow tracking one or more tables can be created via createFlow. Once the Flow stream starts being collected, if a database operation changes one of the tables that the Flow was created from, then such table is considered 'invalidated' and the Flow will emit a new value.

Summary

Nested types

public abstract class InvalidationTracker.Observer

An observer that can listen for changes in the database by subscribing to an InvalidationTracker.

Public methods

void

Adds the given observer to the observers list and it will be notified if any table it observes changes.

final @NonNull Flow<@NonNull Set<@NonNull String>>
createFlow(@NonNull String tables, boolean emitInitialState)

Creates a Flow that tracks modifications in the database and emits sets of the tables that were invalidated.

final void

Refresh created Flows asynchronously, emitting new values on those whose tables have been invalidated.

void

Enqueues a task to refresh the list of updated tables.

void

Removes the observer from the observers list.

Public methods

addObserver

Added in 2.0.0
@WorkerThread
public void addObserver(@NonNull InvalidationTracker.Observer observer)

Adds the given observer to the observers list and it will be notified if any table it observes changes.

Database changes are pulled on another thread so in some race conditions, the observer might be invoked for changes that were done before it is added.

If the observer already exists, this is a no-op call.

If one of the tables in the Observer does not exist in the database, this method throws an IllegalArgumentException.

This method should be called on a background/worker thread as it performs database operations.

Parameters
@NonNull InvalidationTracker.Observer observer

The observer which listens the database for changes.

createFlow

public final @NonNull Flow<@NonNull Set<@NonNull String>> createFlow(@NonNull String tables, boolean emitInitialState)

Creates a Flow that tracks modifications in the database 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.invalidationTracker.createFlow("Artist").map { _ ->
val artists = artistsDao.getAllArtists()
val tours = tourService.fetchStates(artists.map { it.id })
associateTours(artists, tours, from, to)
}
}
Parameters
@NonNull String tables

The name of the tables or views to track.

boolean emitInitialState

Set to false if no initial emission is desired. Default value is true.

refreshAsync

Added in 2.7.0-rc02
public final void refreshAsync()

Refresh created Flows asynchronously, emitting new values on those whose tables have been invalidated.

This function should be called after any write operation is performed on the database, such that tracked tables and its associated flows are notified if invalidated. In most cases Room will call this function automatically but if a write operation is performed on the database via another connection or through RoomDatabase.useConnection you might need to invoke this function manually to trigger invalidation.

refreshVersionsAsync

Added in 2.0.0
public void refreshVersionsAsync()

Enqueues a task to refresh the list of updated tables.

This method is automatically called when RoomDatabase.endTransaction is called but if you have another connection to the database or directly use androidx.sqlite.db.SupportSQLiteDatabase, you may need to call this manually.

See also
refreshAsync

removeObserver

Added in 2.0.0
@WorkerThread
public void removeObserver(@NonNull InvalidationTracker.Observer observer)

Removes the observer from the observers list.

This method should be called on a background/worker thread as it performs database operations.

Parameters
@NonNull InvalidationTracker.Observer observer

The observer to remove.