Dispatchers Across Platforms
Choose dispatchers that exist on Android and iOS.
Dispatchers Across Platforms is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 2 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.
Where Work Actually Runs
A coroutine still runs on some thread. A dispatcher decides which thread or pool it uses, and that choice matters on each platform.
Meet Dispatchers.Default
Use Dispatchers.Default for CPU-heavy work like parsing or sorting. It exists on Android and iOS, so shared code can rely on it.
withContext(Dispatchers.Default) { sort(data) }Dispatchers.Main for UI
Dispatchers.Main runs on the platform UI thread. It is available in common code, but only update screens from here.
withContext(Dispatchers.Main) { render(state) }The IO Dispatcher Gap
Watch out: Dispatchers.IO exists on the JVM and Android but not in plain common code on every target. Do not assume it is everywhere.
Stay in Common-Safe Land
For portable shared logic, prefer Default and Main. They are guaranteed across the targets KMP commonly supports.
Switching with withContext
You move work between threads using withContext. Compute on Default, then hop to Main to publish the result, all in one function.
val r = withContext(Dispatchers.Default) { heavy() }Dispatchers Are Pluggable
A dispatcher is just a CoroutineContext element. You can pass one in, which makes shared code easy to configure and test.
class Loader(val io: CoroutineDispatcher)Inject for Testability
Passing the dispatcher in lets tests swap a deterministic one. Hard-coding it makes shared async logic harder to test. 🧪
iOS Runs Them Too
On iOS these dispatchers map onto the right native queues automatically. Your shared code does not care about GCD details.
One Choice, Both Platforms
Because the dispatcher names are common, the same line schedules work correctly on Android and iOS. No platform branches in your logic. ✨
Pick the Right Tool
Rule of thumb: Default for computation, Main for UI updates, and inject anything IO-like so it stays portable. 🎯
Quick Check
Pick the dispatcher choice that is safest for shared KMP code.
Recap
You learned to pick dispatchers wisely: Default for computation, Main for UI, and inject IO-style ones since they are not on every target. 🎉
Frequently asked questions
Is the “Dispatchers Across Platforms” lesson free?
Yes — the full text of “Dispatchers Across Platforms” 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 “Dispatchers Across Platforms”?
Choose dispatchers that exist on Android and iOS. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Dispatchers Across Platforms” 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
- suspend Functions That Work Everywhere
- Dispatchers Across Platforms
- CoroutineScope & Lifecycle
- withContext & Structured Concurrency