Collect Flows from Swift
Bridge StateFlow to SwiftUI without leaks.
Collect Flows from Swift is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 3 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.
Flows Are Trickier
A Kotlin Flow emits many values over time. Swift has no built-in way to collect it, so you need a small bridge.
val users: StateFlow<List<User>>Why Not Just Await
Await fits a single result, but a Flow is a stream. You must keep listening, which means collecting inside a coroutine.
Expose a Collect Helper
A common pattern is a shared helper that takes a callback and collects the Flow, calling Swift each time a value arrives.
fun watch(onEach: (User) -> Unit)Wrap in ObservableObject
On the Swift side, an ObservableObject holds the latest value as a published property so SwiftUI redraws on each emission.
class UserStore: ObservableObject {
@Published var users = []
}Start Collecting
When the wrapper appears, call the shared watch helper and assign each emitted value to the published property.
repo.watch { self.users = $0 }Keep the Job
The collect helper returns a cancellation handle. Store that job so you can stop the stream when the view goes away.
Cancel on Disappear
In onDisappear, call cancel on the saved job so the coroutine stops and you avoid a leaked, forever-running collector.
job.cancel()StateFlow Has a Value
A StateFlow always holds a current value, so Swift can read its initial state immediately before any new emissions arrive.
Threading Matters
Make sure each value reaches Swift on the main thread, since updating SwiftUI state off the main thread causes problems.
Let SKIE Do It
The library SKIE turns Flows into Swift AsyncSequence, so you can for-await over them and skip writing manual collectors.
One Stream, Live UI
With the bridge in place, your shared Flow drives SwiftUI live: every emission updates the screen, just like on Android.
Quick Check
Let us confirm the safe way to consume a Flow from Swift.
Recap
Bridge a Flow to Swift with a collect helper plus an ObservableObject, update on the main thread, and cancel the job to avoid leaks. 🚀
Frequently asked questions
Is the “Collect Flows from Swift” lesson free?
Yes — the full text of “Collect Flows from Swift” 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 from Swift”?
Bridge StateFlow to SwiftUI without leaks. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Collect Flows from Swift” 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
- How Kotlin Maps to Objective-C
- Suspend Functions as Swift async
- Collect Flows from Swift
- @ObjCName & Swift-Friendly APIs