Kotlin Multiplatform, from the compiler up · Puzzle 6 · 7 of 10
Whose thread is it?
Your shared code uses coroutines and Dispatchers.Main. On Android, that's the main Looper. On iOS there's no Looper. Here's where your coroutines run, and the one thing Swift can't do to them.
The problem
In puzzle 4, a suspend function crossed into Swift as a function with a completion handler. Inside, your shared code launches coroutines on Dispatchers.Main and Dispatchers.IO, as it would on Android. On Android, Main means the main thread's Looper. An iPhone has no Looper. Which thread runs your coroutines on iOS, and what happens when the Swift side gives up? (A Swift Task is roughly a coroutine, and await is where it waits.)
See it
Your coroutine does its network work on
Dispatchers.IO: one of Kotlin's own worker threads.Step 1 of 4It resumes on
Dispatchers.Main. On iOS that's backed by the main queue, so it runs on the main thread, like Android's Looper.Step 2 of 4The Swift task waiting for it is cancelled: the user left. But the plain bridge can't pass cancellation across, and the coroutine keeps running. Predict: what stops it?
Step 3 of 4Owning the scope: the shared view model, whose coroutine is still on
Dispatchers.IO, exposesclose(), the Swift side calls it when the screen goes away, and the coroutine is cancelled.Step 4 of 4
The building block
On iOS, Dispatchers.Main is backed by the main queue: iOS's queue of tasks for the main thread, the one UIKit's run loop works through (the iOS course, puzzles 1 and 2). So a coroutine that resumes on Dispatchers.Main runs on the main thread, exactly where screen work belongs, just as on Android. Dispatchers.Default and Dispatchers.IO run on Kotlin's own pools of worker threads.
The surprise is cancellation. On Android, cancelling a scope stops its coroutines. From Swift, a Kotlin suspend function arrives as a function with a completion handler, and that bridge has no way to pass a cancellation through. So when the Swift task that's waiting is cancelled, say the user leaves the screen, the Kotlin coroutine keeps going: the network call runs to the end, and the Swift side still waits for it, for a screen that's already gone.
Two ways out. Libraries such as SKIE generate real Swift async functions that do pass cancellation through. Or your shared code owns the scope itself: a shared class creates its CoroutineScope, and exposes a close() that the Swift side calls when the screen goes away, which cancels everything in it.
The analogy
The same kitchen from module 1. On iOS the head chef's station is still the main counter (the main queue), and the prep cooks are still in the back. But when a diner on the Swift side walks out, nobody tells the kitchen: the order is cooked anyway, unless the kitchen itself is told to close the ticket.
Check yourself
Sources
- kotlinx.coroutines API: Dispatchers.Main ("On Native Darwin-based targets, it is a dispatcher backed by Darwin's main queue.")
- SKIE: Suspend functions ("suspend functions have to be exposed as Obj-C callback functions, which can be called from Swift using the async/await syntax. However, this is just Swift's syntax sugar. Swift still handles these functions as callback functions that do not support cancellation.")
What you now know
On iOS, Dispatchers.Main is backed by the main queue, so a coroutine resumed there runs on the main thread, like on Android. Default and IO run on Kotlin's own worker threads. If Swift calls a suspend function without a bridge library, cancelling the Swift task doesn't cancel the Kotlin coroutine, so the shared code must own the scope.
Answer every question above correctly to unlock the next puzzle.
Something wrong or unclear on this page? Tell me: a puzzle that didn't click is a bug in the puzzle.