Ergebnisse als Flow bereitstellen
Repository-Daten reaktiv an beide UIs streamen
Ergebnisse als Flow bereitstellen ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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 Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Beyond One-Shot Calls
A suspend function returns once, but real screens want updates over time. That is exactly what Flow gives you.
Return a Flow
Change the repository to return a Flow of your domain type, so callers can react to a stream of results.
fun observeUsers(): Flow<List<User>>Emit With flow Builder
Build a stream using the flow builder and call emit to push each new value to whoever is collecting.
fun observeUsers() = flow { emit(getUsers()) }Suspend Inside Flow
Inside the builder you can freely call suspend functions, so your network fetch fits right in.
Combine With Local Data
A Flow can emit cached data first, then a fresh network result, giving the UI an instant view that updates.
flow {
emit(loadCached())
emit(fetchFresh())
}Map the Stream
Use the map operator on the Flow to transform each emission before the UI ever sees it.
observeUsers().map { it.sortedBy(User::name) }Catch Errors in the Flow
Wrap the stream with catch so a network failure becomes a graceful emission instead of a crash.
observeUsers().catch { emit(emptyList()) }Collect on Each Platform
Both apps collect the Flow: Compose with collectAsState, and SwiftUI through a small bridge.
scope.launch { repo.observeUsers().collect { render(it) } }Still Shared, Still Clean
The Flow logic lives in commonMain, so both platforms share one reactive data source with no copies.
A Single Source of Truth
Exposing data as a Flow gives the whole app one source of truth that everyone observes and trusts.
The Reactive Repository
Here is the repository as a stream: it emits fresh data and never lets a failure break the collector.
fun observeUsers(): Flow<List<User>> = flow {
emit(api.getUsers().toDomain())
}.catch { emit(emptyList()) }Quick Check
Why expose repository data as a Flow instead of a single suspend return?
Recap
Returning a Flow turns the repository into a reactive source: it emits updates over time, both apps collect it, and errors stay contained.
Häufig gestellte Fragen
Ist die Lektion „Ergebnisse als Flow bereitstellen“ kostenlos?
Ja — der vollständige Text von „Ergebnisse als Flow bereitstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Ergebnisse als Flow bereitstellen“?
Repository-Daten reaktiv an beide UIs streamen Du übst Kotlin Multiplatform 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 Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform 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 4 von 4.
Wie lange dauert die Lektion „Ergebnisse als Flow bereitstellen“?
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 Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform 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
- Ein Repository-Interface definieren
- Die API hinter dem Repository kapseln
- DTOs auf Domain-Models abbilden
- Ergebnisse als Flow bereitstellen