CoroutineScope & Lifecycle
Tie shared async work to the right scope and cancel it.
CoroutineScope & Lifecycle 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.
Coroutines Need a Home
Every coroutine belongs to a CoroutineScope. The scope decides how long the work may live and when it should stop.
Why Scope Equals Lifecycle
Tie a scope to a screen or session. When that thing ends, the scope cancels its coroutines so no work runs against a dead UI.
Launch Starts the Work
You begin a coroutine with launch on a scope. It runs the block and tracks it as part of that scope.
scope.launch { refresh() }Build a Scope in Shared Code
Create one with a Job and a dispatcher. This shared scope can drive async work for a ViewModel both apps use.
val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)SupervisorJob Isolates Failures
A SupervisorJob stops one failed child from cancelling its siblings. One broken request will not kill the whole screen.
Cancel to Clean Up
Call cancel when the owner goes away. Pending network or timer coroutines stop immediately, preventing leaks and stray updates.
fun onCleared() { scope.cancel() }Cancellation Is Cooperative
Coroutines check for cancellation at suspension points. Your suspend functions stop cleanly, which is why you rarely need manual flags.
Android Gives You One
On Android a ViewModel already offers viewModelScope. It cancels automatically when the ViewModel is cleared, so you reuse it.
viewModelScope.launch { load() }iOS Needs Manual Cancel
On iOS there is no automatic scope. You hold your shared scope and cancel it from Swift when the view disappears. 📱
Never Use GlobalScope
Avoid GlobalScope: it lives forever and ignores lifecycles. Its coroutines leak and may update screens that no longer exist.
Scope It, Then Forget It
Pick the right scope once and cancellation becomes free. Lifecycle-aware scopes keep shared async work safe on both platforms. 🎯
Quick Check
Let us confirm how scopes guard your coroutines.
Recap
You learned that a CoroutineScope ties async work to a lifecycle: launch starts it, cancel cleans it up, and you avoid GlobalScope. 🎉
Frequently asked questions
Is the “CoroutineScope & Lifecycle” lesson free?
Yes — the full text of “CoroutineScope & Lifecycle” 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 “CoroutineScope & Lifecycle”?
Tie shared async work to the right scope and cancel it. 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 “CoroutineScope & Lifecycle” 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