# Under the Hood: How Room 3.0 and SQLDelight 2.x Detect Database Changes

DevFeed: [Under the Hood: How Room 3.0 and SQLDelight 2.x Detect Database Changes](<https://devfeed.tech/articles/under-the-hood-how-room-3-0-and-sqldelight-2-x-detect-database-changes-107102.md>)

Original publisher: [Read original article](<https://proandroiddev.com/under-the-hood-how-room-3-0-and-sqldelight-2-x-detect-database-changes-51e7a9b716dc?source=rss----c72404660798---4>)

Author: chanzmao

Published: 2026-10-29T17:35:32Z

Content type: comparison

Language: en

Sources: [ProAndroidDev](<https://devfeed.tech/sources/proandroiddev-medium.md>)

Topics: [sqldelight](<https://devfeed.tech/topics/sqldelight.md>), [Query (disambiguation)](<https://devfeed.tech/topics/query.md>)

Tags: [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [android-development](<https://devfeed.tech/tags/android-development.md>), [changes](<https://devfeed.tech/tags/changes.md>), [database](<https://devfeed.tech/tags/database.md>), [flow](<https://devfeed.tech/tags/flow.md>), [kotlin-coroutines](<https://devfeed.tech/tags/kotlin-coroutines.md>), [mobile-app-development](<https://devfeed.tech/tags/mobile-app-development.md>), [notifications](<https://devfeed.tech/tags/notifications.md>), [query](<https://devfeed.tech/tags/query.md>), [room](<https://devfeed.tech/tags/room.md>), [software-architecture](<https://devfeed.tech/tags/software-architecture.md>), [sqldelight](<https://devfeed.tech/tags/sqldelight.md>), [tables](<https://devfeed.tech/tags/tables.md>)

## AI overview

Room 3.0 observes table invalidations: its Flow runs the query when collection starts, then reruns it after a relevant table change. This can rerun a query even when its result is unchanged; cancellation stops the observation. SQLDelight 2.x attaches a listener to a Query. `Query.asFlow()` emits the query as a notification, while downstream operators such as `mapToList()` execute SQL and fetch rows; listener cleanup occurs when collection closes or is cancelled. Neither mechanism avoids reading from the database after a relevant notification. The text cautions that SQLDelight's query-level listener does not guarantee fewer executions or prove that only affected queries are notified; behavior depends on notification and driver implementation details.

## Source excerpt

Room and SQLDelight both let us observe database queries as Kotlin Flows. When the underlying data changes, the Flow emits an update, allowing the UI to react without manually refreshing the data. Image created by AI The result looks similar from the outside, but the two libraries handle change notifications differently. Room relies on InvalidationTracker to monitor database tables. SQLDelight uses listeners attached to individual Query objects. Let's look at how these mechanisms work internally and what the differences mean in practice. How Room 3.0 Monitors Database Changes Room uses InvalidationTracker to keep track of changes to database tables. When a DAO query returns a Flow, Room uses this mechanism to determine when the query should run again. The important detail is that Room tracks which tables a query depends on, rather than tracking changes to the query result itself. What happens behind the scenes? The following example illustrates the basic idea. It is simplified pseudocode, not Room's actual implementation. fun <T> createRoomQueryFlow( tracker: InvalidationTracker, tables: Array<String>, queryBlock: suspend () -> T ): Flow<T> = flow { emit(queryBlock()) tracker.createFlow(*tables).collect { emit(queryBlock()) } } There are two things worth noting here. The query runs before change monitoring begins. When collection starts, queryBlock() fetches the current data and emits the result. The Flow then observes invalidation events from InvalidationTracker. A table change can trigger another query execution. Suppose a query reads from the users table. When Room detects a relevant change to that table, the Flow can emit another result by executing the query again. Room does not need to compare the old and new query results. It uses table invalidation as a signal that the result may have changed. This approach keeps the mechanism straightforward, but it has a trade-off: a change to a table can trigger re-execution even when the actual result of a particular que