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

A coroutine on Dispatchers.IO runs on a Kotlin worker thread and resumes on Dispatchers.Main, which on iOS is the main queue on the main thread; a Swift task is cancelled but the Kotlin coroutine keeps running; closing the shared scope cancels it main thread main queue Kotlin worker coroutine Dispatchers.Main = main queue Swift task: cancelledkeeps running viewModel.close()
  1. Your coroutine does its network work on Dispatchers.IO: one of Kotlin's own worker threads.

    Step 1 of 4
  2. It 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 4
  3. The 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 4
  4. Owning the scope: the shared view model, whose coroutine is still on Dispatchers.IO, exposes close(), 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

Your shared code resumes a coroutine on Dispatchers.Main on iOS. Which thread runs it?

Right. Main is still the main thread on iOS.

Not quite. The dispatcher decides, not the caller: Main means the main queue.

Not quite. On iOS, Main is backed by the main queue, so it still runs on the main thread.

A Swift task awaits your suspend function, then is cancelled. You use no bridge library. What happens to the Kotlin coroutine?

Not quite. The plain bridge is a completion handler, which has no way to carry the cancellation across.

Right. Without a bridge, cancellation doesn't cross.

Not quite. Nothing pauses it; it runs to the end, and nothing stops it.

What's the difference between Dispatchers.Main on Android and on iOS?

Right. Different machinery, same thread.

Not quite. UIKit has a main thread too; Main runs there on both platforms.

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.