0Pricing
Kotlin Multiplatform Academy · Lesson

Collect Flows on Android & iOS

Bridge Flow to Compose and to Swift safely.

Collect Flows on Android & iOS is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Apps, One Stream

Your shared code exposes a Flow, but each platform consumes it differently. The trick is collecting that one stream natively on Android and on iOS.

Compose Loves Flow

On Android, Jetpack Compose turns a Flow into state with collectAsState. Your composable redraws automatically whenever a new value arrives.

val state by viewModel.uiState
    .collectAsState()

Respect the Lifecycle

Prefer collectAsStateWithLifecycle so collection pauses when the screen is in the background. It saves battery and avoids needless work.

val state by viewModel.uiState
    .collectAsStateWithLifecycle()

Swift Cannot See suspend

Plain Flow does not bridge to Swift, because collect is a suspend call. Swift needs a callback-friendly wrapper instead.

Wrap Flow for iOS

A common fix is a small wrapper that subscribes and returns a cancel handle. iOS calls a closure on each value and stops when done.

fun watch(
    onEach: (UiState) -> Unit
): Closeable

Use a CoroutineScope

The wrapper launches collection inside a CoroutineScope it owns. Closing the handle cancels that scope so nothing leaks after the screen is gone.

SwiftUI Subscribes

From SwiftUI you call the wrapper in onAppear, push each value into an ObservableObject, and cancel in onDisappear.

SKIE Smooths the Bridge

Tools like SKIE generate Swift-friendly Flow APIs for you, so iOS devs collect with native async sequences and skip hand-written wrappers.

Always Cancel

However you bridge, make sure collection cancels when the screen disappears. An orphaned collector keeps running and quietly wastes resources.

Logic Stays Shared

Notice the pattern: only the thin collection layer is per platform. The data, operators, and state all stay in shared code where they belong. 🤝

Same Data, Native Feel

Both apps now react to the identical stream, yet each uses its native UI toolkit. That balance is exactly what KMP is built for.

Quick Check

Think about why iOS needs extra help.

Recap

You collected one shared stream two ways: Compose with collectAsStateWithLifecycle, and SwiftUI through a wrapper or SKIE. Shared logic, native UI. 🤝

Frequently asked questions

Is the “Collect Flows on Android & iOS” lesson free?

Yes — the full text of “Collect Flows on Android & iOS” is free to read here on the web, and the Kotlin Multiplatform Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Collect Flows on Android & iOS”?

Bridge Flow to Compose and to Swift safely. You practise Kotlin Multiplatform Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Collect Flows on Android & iOS” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Flow Basics in Shared Code
  2. StateFlow for Shared UI State
  3. Operators: map, filter, combine
  4. Collect Flows on Android & iOS
← Back to Kotlin Multiplatform Academy