---
title: Live Query&#8201;&#8212;&#8201;Working with Queries
description: Couchbase Lite database data querying concepts -- live queries
pubDate: 2026-08-17T09:53:44.266Z
antora:
  editUrl: https://github.com/couchbase/docs-couchbase-lite/edit/release/3.3/modules/android/pages/query-live.adoc
  xref: xref:3.3@couchbase-lite:android:query-live.adoc[]
---

[Consult the llms.txt file for a full list of contents](/llms.txt)
[View original HTML](/couchbase-lite/3.3/android/query-live.html)

# Live Query&#8201;&#8212;&#8201;Working with Queries

> Description — _Couchbase Lite database data querying concepts — live queries_  
> Related Content — [Predictive Queries](querybuilder.md#lbl-predquery) | [Indexing](indexing.md) | [QueryBuilder](querybuilder.md)

## [](#activating-a-live-query)Activating a Live Query

A live query is a query that, once activated, remains active and monitors the database for changes; refreshing the result set whenever a change occurs. As such, it is a great way to build reactive user interfaces — especially table/list views — that keep themselves up to date.

**So, a simple use case may be:** A replicator running and pulling new data from a server, whilst a live-query-driven UI automatically updates to show the data without the user having to manually refresh. This helps your app feel quick and responsive.

To activate a LiveQuery just add a change listener to the query statement. It will be immediately active. When a change is detected the query automatically runs, and posts the new query result to any observers (change listeners).

Example 1\. Starting a Live Query

* Kotlin
* Java

```Kotlin
val query = QueryBuilder
    .select(SelectResult.all())
    .from(DataSource.collection(collection)) (1)

// Adds a query change listener.
// Changes will be posted on the main queue.
val token = query.addChangeListener { change ->
    change.results?.let { rs ->
        rs.forEach {
            log("results: ${it.keys}")
            /* Update UI */
        }
    } (2)
}
```

```Java
Query query = QueryBuilder
    .select(SelectResult.all())
    .from(DataSource.collection(collection)); (1)

// Adds a query change listener.
// Changes will be posted on the main queue.
ListenerToken token = query.addChangeListener(change -> { (2)
    for (Result result: change.getResults()) {
        Logger.log("results: " + result.getKeys());
        /* Update UI */
    }
});
```

| **1** | Build the query statements                                                                                                                                |
| ----- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **2** | Activate the _live_ query by attaching a listener. Save the token in order to detach the listener and stop the query later — se [Example 2](#ex-qry-stop) |

Example 2\. Stop a LIve Query

* Kotlin
* Java

```Kotlin
token.remove()
```

```Java
token.remove(); (1)
```

| **1** | Here we use the change lister token from [Example 1](#ex-qry-start) to remove the listener. Doing so stops the live query. |
| ----- | -------------------------------------------------------------------------------------------------------------------------- |

## [](#using-kotlin-flows-and-livedata)Using Kotlin Flows and LiveData

Kotlin developers also have the option of using Flows and Live Data to feed query changes to the UI.

Define a live query as a Flow returning a LiveData object and activate an Observer in the View onCreate() function.

```Kotlin
Unresolved include directive in modules/android/pages/query-live.adoc - include::example$snippets/app/src/main/kotlin/com/couchbase/code_snippets/FlowExamples.kt[]
```

## [](#related-content)Related Content

###### [](#)

How to . . .

* [QueryBuilder](querybuilder.md)
* [SQL++ for Mobile](query-n1ql-mobile.md)
* [Live Queries](query-live.md)
* [Full Text Search](fts.md)

.

###### [](#-2)

Learn more . . .

* [SQL++ Mobile - Querybuilder Differences](query-n1ql-mobile-querybuilder-diffs.md)
* [SQL++ Mobile - SQL++ Server Differences](query-n1ql-mobile-server-diffs.md)
* [Query Resultsets](query-resultsets.md)
* [Query Troubleshooting](query-troubleshooting.md)
* [Live Queries](query-live.md)
* [Databases](database.md)
* [Documents](document.md)
* [Blobs](blob.md)

.

###### [](#-3)

Dive Deeper . . .

[Mobile Forum](https://forums.couchbase.com/c/mobile/14) | [Blog](https://blog.couchbase.com/) | [Tutorials](https://docs.couchbase.com/tutorials/)

.