InvalidationTracker



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

Observers contain one or more tables and are added to the tracker via subscribe. Once an observer is subscribed, if a database operation changes one of the tables the observer is subscribed to, then such table is considered 'invalidated' and Observer.onInvalidated will be invoked on the observer. If an observer is no longer interested in tracking modifications it can be removed via unsubscribe.

Summary

Nested types

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

Public functions

open Unit

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

android
Unit

Refresh subscribed observers asynchronously, invoking Observer.onInvalidated on those whose tables have been invalidated.

Cmn
android
N
open Unit

Enqueues a task to refresh the list of updated tables.

android
open Unit

Removes the observer from the observers list.

android

Public functions

addObserver

@WorkerThread
open fun addObserver(observer: InvalidationTracker.Observer): Unit

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
observer: InvalidationTracker.Observer

The observer which listens the database for changes.

refreshAsync

fun refreshAsync(): Unit

Refresh subscribed observers asynchronously, invoking Observer.onInvalidated 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 observers are notified if invalidated.

refreshVersionsAsync

open fun refreshVersionsAsync(): Unit

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.

removeObserver

@WorkerThread
open fun removeObserver(observer: InvalidationTracker.Observer): Unit

Removes the observer from the observers list.

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

Parameters
observer: InvalidationTracker.Observer

The observer to remove.