ResultsをFlowとして公開する
Repositoryのデータを両方のUIにリアクティブに配信します
「ResultsをFlowとして公開する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはKotlin Multiplatform Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ Kotlin — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 30
- レッスン
- 120
よくある質問
「ResultsをFlowとして公開する」レッスンは無料ですか?
はい。「ResultsをFlowとして公開する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「ResultsをFlowとして公開する」で何を学びますか?
Repositoryのデータを両方のUIにリアクティブに配信します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ResultsをFlowとして公開する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Repository Interfaceを定義する
- Repositoryの背後にAPIをラップする
- DTOをDomain Modelsにマッピングする
- ResultsをFlowとして公開する