Reaktive Abfragen mit Flow-Rückgabe
Beobachten Sie die Datenbank in Compose.
Reaktive Abfragen mit Flow-Rückgabe ist eine kostenlose Jetpack Compose Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Jetpack Compose Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Reaktive Abfragen mit Flow-Rückgabe“ kostenlos?
Ja — der vollständige Text von „Reaktive Abfragen mit Flow-Rückgabe“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Jetpack Compose Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Jetpack Compose Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Reaktive Abfragen mit Flow-Rückgabe“?
Beobachten Sie die Datenbank in Compose. Du übst Jetpack Compose Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Jetpack Compose Academy zu starten?
Keine Vorkenntnisse erforderlich. Jetpack Compose Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „Reaktive Abfragen mit Flow-Rückgabe“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Jetpack Compose Academy-Lektion Code schreiben und ausführen?
Ja. Jede Jetpack Compose Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Entities, DAOs und die Datenbank
- Reaktive Abfragen mit Flow-Rückgabe
- Das Repository-Pattern
- Offline-First-Caching-Strategie