0Pricing
Kotlin Multiplatform Academy · Lezione

Osservare le tabelle come Flow

Ricevere aggiornamenti reattivi quando cambia il database

Osservare le tabelle come Flow è una lezione Kotlin Multiplatform Academy gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Kotlin Multiplatform Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. ✅

Domande Frequenti

La lezione «Osservare le tabelle come Flow» è gratuita?

Sì — il testo completo di «Osservare le tabelle come Flow» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Kotlin Multiplatform Academy, passa a CoddyKit PRO. Il corso Kotlin Multiplatform Academy include 4 lezioni in totale.

Cosa imparerò in «Osservare le tabelle come Flow»?

Ricevere aggiornamenti reattivi quando cambia il database Eserciti Kotlin Multiplatform Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Kotlin Multiplatform Academy?

Non è richiesta alcuna esperienza precedente. Kotlin Multiplatform Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Osservare le tabelle come Flow»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Kotlin Multiplatform Academy?

Sì. Ogni lezione Kotlin Multiplatform Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Aggiungere SQLDelight e scrivere gli schemi .sq
  2. Configurare SqlDriver per le piattaforme
  3. Inserire, interrogare e aggiornare le righe
  4. Osservare le tabelle come Flow
← Torna a Kotlin Multiplatform Academy