0Pricing
Kotlin Multiplatform Academy · Lesson

Observe Tables as Flow

Get reactive updates when the database changes.

Observe Tables as Flow is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Observe Instead of Poll

Re-running a query by hand misses changes. SQLDelight can push fresh results to you whenever the table changes.

Add the Coroutines Extension

Reactive queries need the coroutines-extensions artifact, which adds Flow support on top of your generated queries.

implementation("app.cash.sqldelight:coroutines-extensions:2.0.2")

Turn a Query into a Flow

Call asFlow on any select query to get a Flow that re-emits whenever the underlying table is written.

val flow = queries.selectAll().asFlow()

Map to Results

asFlow gives a query holder, so chain mapToList to emit a ready Kotlin list each time the data changes.

val players = queries.selectAll()
    .asFlow()
    .mapToList(Dispatchers.IO)

Single-Row Streams

For one row use mapToOneOrNull, which emits the row or null and updates automatically as that row changes.

queries.selectById(1)
    .asFlow()
    .mapToOneOrNull(Dispatchers.IO)

Collect in Shared Code

You consume the stream by collecting it. Every insert, update or delete to that table triggers a fresh emission.

players.collect { list -> render(list) }

Expose It as StateFlow

Wrap the Flow in a StateFlow so your shared ViewModel holds the latest list as a single source of truth.

val state = players.stateIn(scope, SharingStarted.Eagerly, emptyList())

Android Collects with Compose

On Android, collectAsState turns the StateFlow into Compose state, so the list recomposes automatically on every change.

val list by viewModel.state.collectAsState()

iOS Collects from Swift

On iOS you observe the same StateFlow from Swift and update SwiftUI, so both apps stay in sync with the database.

One Write, Both UIs Update

Insert a row anywhere and every active Flow re-emits. The database becomes the live source driving both platforms.

Pick the Right Dispatcher

Run mapping off the main thread by passing a background dispatcher, keeping the UI smooth while queries run.

Quick Check

You collect selectAll().asFlow().mapToList(...). When does it emit again?

Recap: Live Data

You turned queries into Flows, mapped them to results, exposed StateFlow, and let both UIs update on every write. Your shared database is now reactive. ✅

Frequently asked questions

Is the “Observe Tables as Flow” lesson free?

Yes — the full text of “Observe Tables as Flow” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Observe Tables as Flow”?

Get reactive updates when the database changes. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Observe Tables as Flow” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Add SQLDelight & Write .sq Schemas
  2. Platform SqlDriver Setup
  3. Insert, Query & Update Rows
  4. Observe Tables as Flow
← Back to Kotlin Multiplatform Academy