# InvalidationTracker

Artifact: [androidx.room3:room3-runtime](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/InvalidationTracker.+class:androidx.room3.InvalidationTracker) Added in [3.0.2](https://developer.android.com/jetpack/androidx/releases/room3#3.0.2)

*** ** * ** ***


```
public final class InvalidationTracker
```

<br />

*** ** * ** ***

The invalidation tracker keeps track of tables modified by queries and notifies its created `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`s about such modifications.

A `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` tracking one or more tables can be created via `https://developer.android.com/reference/androidx/room3/InvalidationTracker#createFlow(kotlin.Array,kotlin.Boolean)`. Once the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` stream starts being collected, if a database operation changes one of the tables that the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` was created from, then such table is considered 'invalidated' and the `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` will emit a new value.

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/util/Set.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html>>` | `https://developer.android.com/reference/androidx/room3/InvalidationTracker#createFlow(kotlin.Array,kotlin.Boolean)(@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html... tables, boolean emitInitialState)` Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` that tracks modifications in the database and emits sets of the tables that were invalidated. |
| `final void` | `https://developer.android.com/reference/androidx/room3/InvalidationTracker#refreshAsync()()` Refresh created `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`s asynchronously, emitting new values on those whose tables have been invalidated. |

## Public methods

### createFlow

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

Creates a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` that tracks modifications in the database and emits sets of the tables that were invalidated.

The `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` will emit at least one value, a set of all the tables registered for observation to kick-start the stream unless `https://developer.android.com/reference/androidx/room3/InvalidationTracker#createFlow(kotlin.Array,kotlin.Boolean)` is set to `false`.

If one of the tables to observe does not exist in the database, this functions throws an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-illegal-argument-exception/index.html`.

The returned `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html` 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 |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html... 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 [3.0.2](https://developer.android.com/jetpack/androidx/releases/room3#3.0.2)

```
public final void refreshAsync()
```

Refresh created `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-flow/index.html`s 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 `https://developer.android.com/reference/androidx/room3/RoomDatabase#useConnection(kotlin.Boolean,kotlin.coroutines.SuspendFunction1)` you might need to invoke this function manually to trigger invalidation.