Query reattive che restituiscono Flow
Osservi il database in Compose.
Query reattive che restituiscono Flow è una lezione Jetpack Compose Academy gratuita su CoddyKit. Questa è la lezione 2 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 Jetpack Compose Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
Static Reads Go Stale
A one-time query gives you data once, but the table keeps changing. You want the UI to refresh whenever rows are added or removed.
Return a Flow Instead
Change a DAO query to return a Flow. Room now emits a fresh list every single time the underlying table changes. 🔄
@Query("SELECT * FROM Note")
fun observeAll(): Flow<List<Note>>No suspend on Flow Queries
A Flow query is not marked suspend. It returns the stream immediately, and emissions arrive as the data updates over time.
Room Watches the Tables
Behind the scenes Room tracks which tables a query touches. When any of them changes, it re-runs the query and emits a new value.
Collect the Flow Safely
In your ViewModel, collect the Flow inside a coroutine. Use viewModelScope so collection stops when the ViewModel clears.
viewModelScope.launch {
dao.observeAll().collect { notes -> /* update state */ }
}Turn Flow Into State
Convert the stream into observable UI state with stateIn, giving an initial value so the screen has something to show at once.
val notes = dao.observeAll().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())Observe It in Compose
In the Composable read the StateFlow with collectAsStateWithLifecycle. The list recomposes automatically on every change.
val notes by viewModel.notes.collectAsStateWithLifecycle()Insert and Watch It Appear
Insert a row anywhere and the Flow emits again, so the new item shows up instantly without you refreshing manually.
Map Inside the Flow
You can transform results with map before they reach the UI, like sorting or filtering, all reactively in the stream.
dao.observeAll().map { it.sortedBy(Note::text) }Flows Beat LiveData Here
Flow integrates cleanly with coroutines and Compose, making it the modern choice over LiveData for reactive Room queries.
One Source, Always Current
With reactive queries the database becomes your single source of truth, and the UI is just a reflection of it at every moment.
Quick Check
What happens when a Room DAO query returns a Flow?
Recap: Reactive Reads
You made queries return Flow, collected them into state, and watched Compose redraw on every change. Next you will organize this behind a repository.
Domande Frequenti
La lezione «Query reattive che restituiscono Flow» è gratuita?
Sì — il testo completo di «Query reattive che restituiscono 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 Jetpack Compose Academy, passa a CoddyKit PRO. Il corso Jetpack Compose Academy include 4 lezioni in totale.
Cosa imparerò in «Query reattive che restituiscono Flow»?
Osservi il database in Compose. Eserciti Jetpack Compose 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 Jetpack Compose Academy?
Non è richiesta alcuna esperienza precedente. Jetpack Compose 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 2 di 4.
Quanto tempo richiede la lezione «Query reattive che restituiscono 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 Jetpack Compose Academy?
Sì. Ogni lezione Jetpack Compose 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
- Entità, DAO e database
- Query reattive che restituiscono Flow
- Il pattern Repository
- Strategia di caching offline-first