{"id":151,"date":"2026-10-17T08:30:20","date_gmt":"2026-10-17T08:30:20","guid":{"rendered":"https:\/\/moiks.com\/blog\/?p=151"},"modified":"2026-11-06T08:48:33","modified_gmt":"2026-11-06T08:48:33","slug":"kotlin-coroutinescope-job-structured-concurrency-explained-for-android","status":"publish","type":"post","link":"https:\/\/moiks.com\/blog\/kotlin-coroutinescope-job-structured-concurrency-explained-for-android\/","title":{"rendered":"Kotlin CoroutineScope, Job &#038; Structured Concurrency Explained for Android"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Kotlin Coroutines make asynchronous programming easier, but launching a coroutine is only the beginning.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In real Android applications, we also need to answer important questions:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Who owns a coroutine?<\/li>\n\n\n\n<li>When should it stop?<\/li>\n\n\n\n<li>What happens when its parent is cancelled?<\/li>\n\n\n\n<li>What happens when one child coroutine fails?<\/li>\n\n\n\n<li>How do multiple coroutines work together?<\/li>\n\n\n\n<li>How can we prevent background work from outliving a screen?<\/li>\n\n\n\n<li>How do we manage independent tasks safely?<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These questions are answered through <strong>CoroutineScope, Job, and Structured Concurrency<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding these concepts is essential for building reliable Android applications using Kotlin, ViewModel, StateFlow, and Jetpack Compose.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we&#8217;ll explore coroutine lifecycle management from the fundamentals to practical Android examples.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What You&#8217;ll Learn<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this tutorial, you&#8217;ll understand:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What CoroutineScope is<\/li>\n\n\n\n<li>What CoroutineContext contains<\/li>\n\n\n\n<li>How Job manages coroutine lifecycles<\/li>\n\n\n\n<li>Parent-child coroutine relationships<\/li>\n\n\n\n<li>Structured concurrency<\/li>\n\n\n\n<li>Coroutine cancellation<\/li>\n\n\n\n<li>Cooperative cancellation<\/li>\n\n\n\n<li><code>join()<\/code> and <code>cancelAndJoin()<\/code><\/li>\n\n\n\n<li><code>coroutineScope()<\/code><\/li>\n\n\n\n<li><code>supervisorScope()<\/code><\/li>\n\n\n\n<li><code>SupervisorJob<\/code><\/li>\n\n\n\n<li>Exception propagation<\/li>\n\n\n\n<li><code>launch()<\/code> vs <code>async()<\/code><\/li>\n\n\n\n<li><code>viewModelScope<\/code><\/li>\n\n\n\n<li><code>lifecycleScope<\/code><\/li>\n\n\n\n<li>Jetpack Compose coroutine management<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s begin!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1. What Is CoroutineScope in Kotlin?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>CoroutineScope<\/code> defines the context in which coroutines are launched.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It provides a structured way to manage related coroutine operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    launch {\n        delay(1000)\n        println(\"Coroutine completed\")\n    }\n\n    println(\"Main operation\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Typical Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Main operation\nCoroutine completed<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>runBlocking<\/code> function creates a coroutine scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside this scope, we launch a child coroutine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parent scope waits for its child to complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Is CoroutineScope Important?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A coroutine scope helps determine:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Coroutine ownership<\/li>\n\n\n\n<li>Execution context<\/li>\n\n\n\n<li>Cancellation behavior<\/li>\n\n\n\n<li>Parent-child relationships<\/li>\n\n\n\n<li>Coroutine lifetime<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Without proper scope management, background operations can continue longer than intended.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2. Understanding CoroutineContext<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Every coroutine has a <code>CoroutineContext<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A CoroutineContext is a collection of elements that describe how a coroutine executes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common elements include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Job<\/li>\n\n\n\n<li>CoroutineDispatcher<\/li>\n\n\n\n<li>CoroutineName<\/li>\n\n\n\n<li>CoroutineExceptionHandler<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    launch(\n        Dispatchers.Default +\n            CoroutineName(\"WorkerCoroutine\")\n    ) {\n\n        println(\n            \"Running on: ${Thread.currentThread().name}\"\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Required Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import kotlinx.coroutines.CoroutineName\nimport kotlinx.coroutines.Dispatchers\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.runBlocking<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Dispatchers.Default<\/code> selects an appropriate thread pool for CPU-oriented work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>CoroutineName<\/code> assigns a descriptive name useful for debugging.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine also has a Job associated with its execution.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineContext is not the same as Android&#8217;s <code>Context<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Android Context provides access to application and platform resources.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineContext controls coroutine execution and lifecycle-related behavior.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3. What Is Job in Kotlin Coroutines?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>Job<\/code> represents a coroutine&#8217;s lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When we call <code>launch()<\/code>, it returns a Job.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val job = launch {\n\n        delay(1000)\n\n        println(\"Task completed\")\n    }\n\n    job.join()\n\n    println(\"Job finished\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Task completed\nJob finished<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>launch()<\/code> function starts a coroutine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The returned Job can be used to manage its lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>join()<\/code> suspends the caller until the Job completes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Common Job Operations<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Operation<\/th><th>Purpose<\/th><\/tr><tr><td><code>start()<\/code><\/td><td>Start a lazily created Job<\/td><\/tr><tr><td><code>cancel()<\/code><\/td><td>Request cancellation<\/td><\/tr><tr><td><code>join()<\/code><\/td><td>Wait for completion<\/td><\/tr><tr><td><code>cancelAndJoin()<\/code><\/td><td>Cancel and wait<\/td><\/tr><tr><td><code>isActive<\/code><\/td><td>Check whether Job is active<\/td><\/tr><tr><td><code>isCancelled<\/code><\/td><td>Check cancellation state<\/td><\/tr><tr><td><code>isCompleted<\/code><\/td><td>Check completion state<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4. Kotlin Job Lifecycle<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Job can move through several states during execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Common states include:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>New<\/li>\n\n\n\n<li>Active<\/li>\n\n\n\n<li>Completing<\/li>\n\n\n\n<li>Cancelling<\/li>\n\n\n\n<li>Cancelled<\/li>\n\n\n\n<li>Completed<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val job = launch(\n        start = CoroutineStart.LAZY\n    ) {\n\n        delay(500)\n\n        println(\"Lazy coroutine executed\")\n    }\n\n    println(\"Before start: ${job.isActive}\")\n\n    job.start()\n\n    println(\"After start: ${job.isActive}\")\n\n    job.join()\n\n    println(\"Completed: ${job.isCompleted}\")\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine is initially created in a lazy state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Calling <code>start()<\/code> schedules it for execution.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After its work finishes, the Job becomes completed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The exact timing of intermediate state observations depends on scheduling.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, <code>isCompleted<\/code> can be true for both successful and cancelled jobs.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">5. Parent and Child Coroutines<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structured concurrency organizes coroutines into parent-child relationships.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val parentJob = launch {\n\n        launch {\n\n            delay(500)\n\n            println(\"Child 1 completed\")\n        }\n\n        launch {\n\n            delay(1000)\n\n            println(\"Child 2 completed\")\n        }\n\n        println(\"Parent body finished\")\n    }\n\n    parentJob.join()\n\n    println(\"All child tasks completed\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Typical Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Parent body finished\nChild 1 completed\nChild 2 completed\nAll child tasks completed<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The parent coroutine launches two children.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Even after the parent&#8217;s body finishes, its Job does not complete until its children finish.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Principle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A parent coroutine normally waits for all of its child coroutines before completing.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This helps maintain predictable task lifetimes.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">6. What Is Structured Concurrency?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Structured concurrency is a programming model that organizes concurrent operations under clearly defined scopes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instead of launching unrelated background tasks, we create a hierarchy of operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>suspend fun loadDashboard() = coroutineScope {\n\n    val profile = async {\n        fetchProfile()\n    }\n\n    val notifications = async {\n        fetchNotifications()\n    }\n\n    println(profile.await())\n    println(notifications.await())\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Assume <code>fetchProfile()<\/code> and <code>fetchNotifications()<\/code> are existing suspend functions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Both operations are children of the same scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The scope waits for both operations to complete.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If a child fails with an ordinary exception, the failure normally cancels the scope and its other children.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Benefits<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Predictable coroutine lifetimes<\/li>\n\n\n\n<li>Easier cancellation<\/li>\n\n\n\n<li>Better exception propagation<\/li>\n\n\n\n<li>Fewer unmanaged background tasks<\/li>\n\n\n\n<li>Clearer ownership<\/li>\n\n\n\n<li>Easier testing<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">7. Understanding coroutineScope()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>coroutineScope()<\/code> function creates a child coroutine scope and suspends until all its children complete.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>suspend fun performTasks() = coroutineScope {\n\n    launch {\n\n        delay(500)\n\n        println(\"Task A completed\")\n    }\n\n    launch {\n\n        delay(1000)\n\n        println(\"Task B completed\")\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Usage<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    performTasks()\n\n    println(\"All tasks finished\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Task A completed\nTask B completed\nAll tasks finished<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>coroutineScope()<\/code> does not block the underlying thread while waiting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It suspends until its child operations finish.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use coroutineScope()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use it when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Several child tasks belong to one operation.<\/li>\n\n\n\n<li>All child tasks must complete.<\/li>\n\n\n\n<li>Failure of one task should normally fail the entire operation.<\/li>\n\n\n\n<li>You need structured concurrent execution.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">8. Coroutine Cancellation<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cancellation is an important part of coroutine lifecycle management.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val job = launch {\n\n        repeat(10) { index -&gt;\n\n            println(\"Working: $index\")\n\n            delay(500)\n        }\n    }\n\n    delay(1200)\n\n    job.cancelAndJoin()\n\n    println(\"Coroutine cancelled\")\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine performs repeated work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After approximately 1.2 seconds, cancellation is requested.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Because <code>delay()<\/code> is cancellation-aware, the coroutine can stop promptly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Cancellation is cooperative.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A coroutine performing continuous CPU-intensive work may need explicit cancellation checks.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">9. Cooperative Cancellation Using isActive<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>isActive<\/code> property can help determine whether a coroutine should continue running.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val job = launch(Dispatchers.Default) {\n\n        while (isActive) {\n\n            println(\"Processing...\")\n\n            delay(300)\n        }\n    }\n\n    delay(1000)\n\n    job.cancelAndJoin()\n\n    println(\"Stopped\")\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The loop continues while the coroutine is active.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When cancellation is requested, the loop exits.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Another Option: ensureActive()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For CPU-intensive loops, <code>ensureActive()<\/code> can explicitly check cancellation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>suspend fun processLargeDataset() {\n\n    withContext(Dispatchers.Default) {\n\n        repeat(1_000_000) {\n\n            ensureActive()\n\n            \/\/ Perform calculation\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Required Import<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import kotlinx.coroutines.ensureActive<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use cancellation checks at reasonable intervals in long-running calculations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Checking cancellation on every trivial operation may be unnecessary.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">10. Understanding cancelAndJoin()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>cancelAndJoin()<\/code> function combines two operations:<\/p>\n\n\n\n<ol start=\"1\" class=\"wp-block-list\">\n<li>Request cancellation.<\/li>\n\n\n\n<li>Suspend until the Job completes.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val job = launch {\n\n        try {\n\n            repeat(10) {\n\n                delay(500)\n\n                println(\"Working...\")\n            }\n\n        } finally {\n\n            println(\"Cleanup completed\")\n        }\n    }\n\n    delay(1000)\n\n    job.cancelAndJoin()\n\n    println(\"Job fully stopped\")\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Cancellation interrupts the suspended operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>finally<\/code> block executes during normal coroutine cancellation cleanup.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The caller waits until the Job has finished.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If cleanup needs to call suspending functions after cancellation, special handling with <code>NonCancellable<\/code> may be required.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use that carefully and only for necessary bounded cleanup.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">11. Exception Propagation in Coroutines<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Coroutine exceptions can affect parent and sibling coroutines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    try {\n\n        coroutineScope {\n\n            launch {\n\n                delay(300)\n\n                throw IllegalStateException(\n                    \"Task failed\"\n                )\n            }\n\n            launch {\n\n                try {\n\n                    delay(1000)\n\n                    println(\"Second task completed\")\n\n                } finally {\n\n                    println(\"Second task stopped\")\n                }\n            }\n        }\n\n    } catch (e: IllegalStateException) {\n\n        println(\"Handled: ${e.message}\")\n    }\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Typical Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Second task stopped\nHandled: Task failed<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The first child throws an exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>coroutineScope<\/code> cancels its remaining children.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The exception propagates to the caller, where it is handled.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Point<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In a regular coroutine scope, a child failure generally cancels its parent scope and sibling operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This is appropriate when tasks are part of one operation that must succeed together.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">12. What Is SupervisorJob?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>SupervisorJob<\/code> provides a different failure relationship between child coroutines.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With a supervisor, the failure of one child does not automatically cancel its sibling coroutines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    val supervisor = SupervisorJob()\n\n    val scope = CoroutineScope(\n        Dispatchers.Default + supervisor\n    )\n\n    val first = scope.launch {\n\n        try {\n\n            delay(300)\n\n            println(\"First task failed\")\n\n        } catch (e: Exception) {\n\n            println(\"Handled failure\")\n        }\n    }\n\n    val second = scope.launch {\n\n        delay(700)\n\n        println(\"Second task completed\")\n    }\n\n    first.join()\n    second.join()\n\n    supervisor.cancel()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example creates a supervisor-owned scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each child can perform its own work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The first child handles a simulated failure locally, while the second completes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example above demonstrates independent child operations, but the first child handles its exception directly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For an actual unhandled child exception, SupervisorJob prevents automatic sibling cancellation, but the failure must still be handled or reported appropriately.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">13. Understanding supervisorScope()<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>supervisorScope()<\/code> function creates a supervised scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Unlike <code>coroutineScope()<\/code>, a child failure does not automatically cancel its siblings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>fun main() = runBlocking {\n\n    supervisorScope {\n\n        val first = launch {\n\n            try {\n\n                delay(300)\n\n                throw IllegalStateException(\n                    \"First task failed\"\n                )\n\n            } catch (e: IllegalStateException) {\n\n                println(\"Handled: ${e.message}\")\n            }\n        }\n\n        val second = launch {\n\n            delay(700)\n\n            println(\"Second task completed\")\n        }\n\n        first.join()\n        second.join()\n    }\n\n    println(\"Supervisor scope completed\")\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Handled: First task failed\nSecond task completed\nSupervisor scope completed<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The first child handles its failure.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The second child continues independently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The supervisor scope waits for its children to finish.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">When to Use supervisorScope()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use it when:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Child operations are independent.<\/li>\n\n\n\n<li>One task failure should not automatically cancel siblings.<\/li>\n\n\n\n<li>Each task has appropriate failure handling.<\/li>\n\n\n\n<li>Partial success is acceptable.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>supervisorScope()<\/code> does not suppress exceptions automatically.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An uncaught exception from a <code>launch<\/code> child can still be reported to the coroutine exception handler.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For <code>async<\/code> children, failures must be handled when awaiting results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, if the supervisor scope&#8217;s own block fails, its children are cancelled.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">14. coroutineScope vs supervisorScope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s compare both functions.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Feature<\/th><th>coroutineScope<\/th><th>supervisorScope<\/th><\/tr><tr><td>Structured concurrency<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Waits for children<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Child failure cancels siblings<\/td><td>Normally yes<\/td><td>No, not automatically<\/td><\/tr><tr><td>Parent cancellation cancels children<\/td><td>Yes<\/td><td>Yes<\/td><\/tr><tr><td>Suitable for<\/td><td>Dependent tasks<\/td><td>Independent tasks<\/td><\/tr><tr><td>Failure handling<\/td><td>Propagates through scope<\/td><td>Child failures handled separately<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Imagine a dashboard that loads:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>User profile<\/li>\n\n\n\n<li>Weather information<\/li>\n\n\n\n<li>Recent notifications<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If all three are required for one combined result, <code>coroutineScope()<\/code> may be appropriate.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If each section can display independently, <code>supervisorScope()<\/code> may be more suitable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Choose based on the relationship between operations, not simply because one option appears more fault-tolerant.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">15. SupervisorJob vs Job<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A regular Job and SupervisorJob differ mainly in how child failures affect the parent and siblings.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Job<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A child failure normally cancels the parent Job and other children.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">SupervisorJob<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A child failure does not automatically cancel the supervisor or sibling children.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important Similarity<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Cancelling the parent Job or SupervisorJob cancels its children.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Supervision changes child-to-parent failure propagation, not ordinary parent-to-child cancellation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">16. Understanding launch() vs async() Failure Handling<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Both <code>launch()<\/code> and <code>async()<\/code> can execute concurrent work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, they expose failures differently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">launch()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A <code>launch<\/code> coroutine does not return a result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">An unhandled exception can be propagated or reported according to its parent and exception-handler configuration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">async()<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An <code>async<\/code> coroutine returns a Deferred.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Its result or failure is observed through <code>await()<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>suspend fun loadValue(): Int = coroutineScope {\n\n    val deferred = async {\n\n        delay(300)\n\n        100\n    }\n\n    deferred.await()\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>async()<\/code> does not automatically isolate failures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Inside a regular coroutineScope, an async child failure can still cancel the parent and siblings, even before the caller invokes <code>await()<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use appropriate supervision and exception handling when independent failure behavior is required.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">17. Android viewModelScope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Android provides <code>viewModelScope<\/code> to manage coroutines associated with a ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class UserViewModel : ViewModel() {\n\n    fun loadUser() {\n\n        viewModelScope.launch {\n\n            delay(1000)\n\n            println(\"User loaded\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine belongs to the ViewModel&#8217;s scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When the ViewModel is cleared, its scope is cancelled.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why Is This Useful?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">It helps prevent work owned by a destroyed ViewModel from continuing unnecessarily.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Practical Uses<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Loading screen data<\/li>\n\n\n\n<li>Saving user settings<\/li>\n\n\n\n<li>Calling repositories<\/li>\n\n\n\n<li>Collecting StateFlow<\/li>\n\n\n\n<li>Processing user actions<\/li>\n\n\n\n<li>Performing screen-related background operations<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>viewModelScope<\/code> is configured with a SupervisorJob by default.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">However, individual coroutine failures still need appropriate handling.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">18. Android lifecycleScope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Android also provides <code>lifecycleScope<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is associated with a LifecycleOwner such as an Activity or Fragment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class MainActivity : ComponentActivity() {\n\n    override fun onCreate(\n        savedInstanceState: Bundle?\n    ) {\n        super.onCreate(savedInstanceState)\n\n        lifecycleScope.launch {\n\n            delay(1000)\n\n            Log.d(\n                \"MainActivity\",\n                \"Coroutine executed\"\n            )\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine belongs to the Activity&#8217;s lifecycle scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The scope is cancelled when the lifecycle reaches the destroyed state.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">viewModelScope vs lifecycleScope<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Scope<\/th><th>Owner<\/th><th>Typical Use<\/th><\/tr><tr><td>viewModelScope<\/td><td>ViewModel<\/td><td>Screen business logic and data loading<\/td><\/tr><tr><td>lifecycleScope<\/td><td>LifecycleOwner<\/td><td>Lifecycle-related UI operations<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>viewModelScope<\/code> for ViewModel-owned work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>lifecycleScope<\/code> for operations genuinely owned by the Activity or Fragment lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For collecting flows only while the UI is visible, consider <code>repeatOnLifecycle()<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">19. Jetpack Compose Coroutine Management<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Jetpack Compose provides coroutine-related APIs such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>LaunchedEffect<\/code><\/li>\n\n\n\n<li><code>rememberCoroutineScope<\/code><\/li>\n\n\n\n<li><code>produceState<\/code><\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These help manage asynchronous operations within Compose.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">LaunchedEffect<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>LaunchedEffect<\/code> for suspend operations tied to composition and effect keys.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun LoadingMessage() {\n\n    var message by remember {\n        mutableStateOf(\"Loading...\")\n    }\n\n    LaunchedEffect(Unit) {\n\n        delay(1000)\n\n        message = \"Completed\"\n    }\n\n    Text(message)\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The effect launches a coroutine when it enters composition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine is cancelled when the effect leaves composition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the effect keys change, the previous coroutine is cancelled and a new one starts.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">20. rememberCoroutineScope in Jetpack Compose<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>rememberCoroutineScope()<\/code> function provides a CoroutineScope tied to the composable&#8217;s presence in composition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is commonly used to launch coroutines from event handlers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun SaveButton() {\n\n    val scope = rememberCoroutineScope()\n\n    var message by remember {\n        mutableStateOf(\"Ready\")\n    }\n\n    Column {\n\n        Text(message)\n\n        Button(\n            onClick = {\n\n                scope.launch {\n\n                    message = \"Saving...\"\n\n                    delay(1000)\n\n                    message = \"Saved\"\n                }\n            }\n        ) {\n\n            Text(\"Save\")\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">When the user taps Save, the event handler launches a coroutine.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The coroutine updates the displayed message.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Best Practice<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For application business logic, prefer calling ViewModel methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use <code>rememberCoroutineScope()<\/code> primarily for UI-specific event-driven work, such as showing snackbars or controlling UI components.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">21. Practical Android Example: Independent Dashboard Tasks<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a repository that loads two independent pieces of dashboard information.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create Models<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class UserProfile(\n    val name: String,\n    val email: String\n)\n\ndata class DashboardStats(\n    val totalProjects: Int,\n    val completedProjects: Int\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the Repository<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class DashboardRepository {\n\n    suspend fun getProfile(): UserProfile {\n\n        delay(1000)\n\n        return UserProfile(\n            name = \"Alex\",\n            email = \"alex@example.com\"\n        )\n    }\n\n    suspend fun getStats(): DashboardStats {\n\n        delay(1500)\n\n        return DashboardStats(\n            totalProjects = 12,\n            completedProjects = 8\n        )\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The repository simulates two asynchronous operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both functions are independent.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In a real application, these might be separate Retrofit API calls.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">22. Build a Dashboard ViewModel with supervisorScope<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll load the profile and statistics independently.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Create UI State<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>data class DashboardUiState(\n    val isLoading: Boolean = true,\n    val profile: UserProfile? = null,\n    val stats: DashboardStats? = null,\n    val profileError: String? = null,\n    val statsError: String? = null\n)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create the ViewModel<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>class DashboardViewModel : ViewModel() {\n\n    private val repository = DashboardRepository()\n\n    private val _uiState = MutableStateFlow(\n        DashboardUiState()\n    )\n\n    val uiState: StateFlow&lt;DashboardUiState&gt; =\n        _uiState.asStateFlow()\n\n    init {\n        loadDashboard()\n    }\n\n    fun loadDashboard() {\n\n        viewModelScope.launch {\n\n            _uiState.value = DashboardUiState(\n                isLoading = true\n            )\n\n            supervisorScope {\n\n                val profileTask = async {\n\n                    try {\n\n                        Result.success(\n                            repository.getProfile()\n                        )\n\n                    } catch (e: CancellationException) {\n\n                        throw e\n\n                    } catch (e: Exception) {\n\n                        Result.failure&lt;UserProfile&gt;(e)\n                    }\n                }\n\n                val statsTask = async {\n\n                    try {\n\n                        Result.success(\n                            repository.getStats()\n                        )\n\n                    } catch (e: CancellationException) {\n\n                        throw e\n\n                    } catch (e: Exception) {\n\n                        Result.failure&lt;DashboardStats&gt;(e)\n                    }\n                }\n\n                val profileResult = profileTask.await()\n\n                val statsResult = statsTask.await()\n\n                _uiState.value = DashboardUiState(\n                    isLoading = false,\n                    profile = profileResult.getOrNull(),\n                    stats = statsResult.getOrNull(),\n                    profileError = if (profileResult.isFailure) {\n                        \"Unable to load profile\"\n                    } else {\n                        null\n                    },\n                    statsError = if (statsResult.isFailure) {\n                        \"Unable to load statistics\"\n                    } else {\n                        null\n                    }\n                )\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The ViewModel starts two independent operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both execute concurrently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Each task handles its own recoverable exceptions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the profile request fails, the statistics request can still succeed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the statistics request fails, the profile can still be displayed.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Important<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The example preserves coroutine cancellation by rethrowing CancellationException.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Result wrappers represent recoverable failures rather than swallowing cancellation.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">23. Jetpack Compose Dashboard Screen<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s create a Compose screen that observes the ViewModel.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>@Composable\nfun DashboardScreen(\n    viewModel: DashboardViewModel = viewModel()\n) {\n\n    val uiState by viewModel.uiState\n        .collectAsStateWithLifecycle()\n\n    Column(\n        modifier = Modifier\n            .fillMaxSize()\n            .padding(24.dp),\n        verticalArrangement = Arrangement.spacedBy(16.dp)\n    ) {\n\n        Text(\n            text = \"My Dashboard\",\n            style = MaterialTheme.typography.headlineMedium\n        )\n\n        if (uiState.isLoading) {\n\n            CircularProgressIndicator()\n\n            Text(\"Loading dashboard...\")\n\n        } else {\n\n            uiState.profile?.let { profile -&gt;\n\n                Card(\n                    modifier = Modifier.fillMaxWidth()\n                ) {\n\n                    Column(\n                        modifier = Modifier.padding(16.dp)\n                    ) {\n\n                        Text(\n                            text = profile.name,\n                            style = MaterialTheme.typography.titleLarge\n                        )\n\n                        Text(profile.email)\n                    }\n                }\n            }\n\n            uiState.profileError?.let { error -&gt;\n\n                Text(\n                    text = error,\n                    color = MaterialTheme.colorScheme.error\n                )\n            }\n\n            uiState.stats?.let { stats -&gt;\n\n                Card(\n                    modifier = Modifier.fillMaxWidth()\n                ) {\n\n                    Column(\n                        modifier = Modifier.padding(16.dp)\n                    ) {\n\n                        Text(\n                            text = \"Project Statistics\",\n                            style = MaterialTheme.typography.titleMedium\n                        )\n\n                        Text(\n                            text = \"Total Projects: ${stats.totalProjects}\"\n                        )\n\n                        Text(\n                            text = \"Completed: ${stats.completedProjects}\"\n                        )\n                    }\n                }\n            }\n\n            uiState.statsError?.let { error -&gt;\n\n                Text(\n                    text = error,\n                    color = MaterialTheme.colorScheme.error\n                )\n            }\n\n            Button(\n                onClick = {\n                    viewModel.loadDashboard()\n                }\n            ) {\n\n                Text(\"Refresh Dashboard\")\n            }\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Required Imports<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import androidx.compose.foundation.layout.Arrangement\nimport androidx.compose.foundation.layout.Column\nimport androidx.compose.foundation.layout.fillMaxSize\nimport androidx.compose.foundation.layout.fillMaxWidth\nimport androidx.compose.foundation.layout.padding\nimport androidx.compose.material3.Button\nimport androidx.compose.material3.Card\nimport androidx.compose.material3.CircularProgressIndicator\nimport androidx.compose.material3.MaterialTheme\nimport androidx.compose.material3.Text\nimport androidx.compose.runtime.Composable\nimport androidx.compose.runtime.getValue\nimport androidx.compose.ui.Modifier\nimport androidx.compose.ui.unit.dp\nimport androidx.lifecycle.ViewModel\nimport androidx.lifecycle.viewModelScope\nimport androidx.lifecycle.viewmodel.compose.viewModel\nimport androidx.lifecycle.compose.collectAsStateWithLifecycle\nimport kotlinx.coroutines.CancellationException\nimport kotlinx.coroutines.async\nimport kotlinx.coroutines.delay\nimport kotlinx.coroutines.launch\nimport kotlinx.coroutines.supervisorScope\nimport kotlinx.coroutines.flow.MutableStateFlow\nimport kotlinx.coroutines.flow.StateFlow\nimport kotlinx.coroutines.flow.asStateFlow<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Expected UI<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>+--------------------------------+\n|                                |\n|          My Dashboard          |\n|                                |\n| +----------------------------+ |\n| | Alex                       | |\n| | alex@example.com           | |\n| +----------------------------+ |\n|                                |\n| +----------------------------+ |\n| | Project Statistics         | |\n| |                            | |\n| | Total Projects: 12         | |\n| | Completed: 8               | |\n| +----------------------------+ |\n|                                |\n|    &#91; Refresh Dashboard ]       |\n|                                |\n+--------------------------------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">How It Works<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Repository<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Provides independent suspend functions for profile and statistics data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>ViewModel<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Launches a coroutine using viewModelScope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>supervisorScope<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Allows independent child tasks to handle their failures separately.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>async and await<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Run both tasks concurrently and retrieve their results.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>StateFlow<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Exposes the dashboard state.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Jetpack Compose<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Observes the state and displays the available data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Production Note<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This mini-project uses simulated data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For production applications, consider:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Dependency injection with Hilt<\/li>\n\n\n\n<li>Retrofit API integration<\/li>\n\n\n\n<li>Explicit error classification<\/li>\n\n\n\n<li>Separate loading states for each dashboard section<\/li>\n\n\n\n<li>Cancelling obsolete refresh requests<\/li>\n\n\n\n<li>Unit testing with kotlinx-coroutines-test<\/li>\n\n\n\n<li>Stable and immutable UI models<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Also, repeated Refresh taps can start overlapping requests in this simplified implementation. A production ViewModel should prevent, cancel, or coordinate overlapping refresh operations.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">24. Common CoroutineScope and Job Mistakes<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 1: Creating Unmanaged CoroutineScopes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid creating new scopes for every operation without managing their lifetimes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 2: Using GlobalScope for Screen Work<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">GlobalScope is not tied to a screen lifecycle.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Prefer viewModelScope or another appropriately owned scope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 3: Ignoring Child Failures<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Unhandled child exceptions can cancel related operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Understand the scope&#8217;s failure behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 4: Assuming SupervisorJob Handles Exceptions Automatically<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SupervisorJob prevents sibling cancellation from child failure, but it does not automatically handle exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 5: Swallowing CancellationException<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Preserve cancellation when handling broad exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 6: Using supervisorScope for Dependent Tasks<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If all child tasks must succeed together, coroutineScope may be more appropriate.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 7: Blocking the Main Thread<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Avoid Thread.sleep() and heavy synchronous work on the main dispatcher.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 8: Launching Coroutines During Recomposition<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Do not launch uncontrolled coroutines directly from composable rendering logic.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mistake 9: Forgetting Coroutine Ownership<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Every long-running operation should have a clearly defined owner and cancellation policy.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">25. Kotlin Structured Concurrency Best Practices<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Follow these recommendations:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use structured concurrency for related tasks.<\/li>\n\n\n\n<li>Prefer lifecycle-aware scopes in Android.<\/li>\n\n\n\n<li>Keep coroutine ownership explicit.<\/li>\n\n\n\n<li>Use coroutineScope for dependent operations.<\/li>\n\n\n\n<li>Use supervisorScope for independent operations.<\/li>\n\n\n\n<li>Handle failures at appropriate boundaries.<\/li>\n\n\n\n<li>Preserve CancellationException.<\/li>\n\n\n\n<li>Avoid unmanaged background work.<\/li>\n\n\n\n<li>Use Dispatchers.IO for blocking I\/O.<\/li>\n\n\n\n<li>Use Dispatchers.Default for CPU-intensive work.<\/li>\n\n\n\n<li>Prefer ViewModel for screen business logic.<\/li>\n\n\n\n<li>Use StateFlow for observable UI state.<\/li>\n\n\n\n<li>Avoid overlapping requests when they are not needed.<\/li>\n\n\n\n<li>Test cancellation and failure scenarios.<\/li>\n\n\n\n<li>Use CoroutineName when useful for debugging.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">These practices help build reliable and maintainable Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">26. Quick Reference<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><th>Concept<\/th><th>Purpose<\/th><\/tr><tr><td>CoroutineScope<\/td><td>Defines coroutine context and ownership<\/td><\/tr><tr><td>CoroutineContext<\/td><td>Contains Job, dispatcher, and other elements<\/td><\/tr><tr><td>Job<\/td><td>Represents coroutine lifecycle<\/td><\/tr><tr><td>launch<\/td><td>Starts coroutine returning Job<\/td><\/tr><tr><td>async<\/td><td>Starts coroutine returning Deferred<\/td><\/tr><tr><td>join<\/td><td>Waits for Job completion<\/td><\/tr><tr><td>cancel<\/td><td>Requests cancellation<\/td><\/tr><tr><td>cancelAndJoin<\/td><td>Cancels and waits<\/td><\/tr><tr><td>isActive<\/td><td>Checks active state<\/td><\/tr><tr><td>ensureActive<\/td><td>Throws when coroutine is cancelled<\/td><\/tr><tr><td>coroutineScope<\/td><td>Structured scope with normal failure propagation<\/td><\/tr><tr><td>supervisorScope<\/td><td>Structured scope with independent child failure behavior<\/td><\/tr><tr><td>SupervisorJob<\/td><td>Supervises child failures<\/td><\/tr><tr><td>viewModelScope<\/td><td>ViewModel-owned coroutine scope<\/td><\/tr><tr><td>lifecycleScope<\/td><td>LifecycleOwner-owned coroutine scope<\/td><\/tr><tr><td>LaunchedEffect<\/td><td>Composition-scoped coroutine effect<\/td><\/tr><tr><td>rememberCoroutineScope<\/td><td>Composition-owned scope for event-driven work<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">27. Practical Exercises<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Try these exercises to strengthen your understanding.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 1: CoroutineScope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a coroutine scope and launch two child coroutines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 2: Job Lifecycle<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a lazy coroutine and inspect its Job state before and after starting.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 3: Parent-Child Relationship<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Launch two children inside a parent coroutine and verify that the parent waits for them.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 4: Cancellation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a repeating coroutine and cancel it using cancelAndJoin().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 5: Cooperative Cancellation<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a CPU-intensive loop and check cancellation using ensureActive().<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 6: coroutineScope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create two dependent tasks where failure of one should cancel the other.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 7: supervisorScope<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create two independent tasks where one can fail without cancelling the other.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 8: SupervisorJob<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a supervisor-owned scope and manage two child operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 9: ViewModel<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a ViewModel that loads data using viewModelScope.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Exercise 10: Jetpack Compose Dashboard<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Build a dashboard that loads profile and statistics data concurrently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Handle loading, success, and error states.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">28. Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is CoroutineScope in Kotlin?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">CoroutineScope defines the context and ownership of coroutines launched within it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is Job in Kotlin Coroutines?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Job represents a coroutine&#8217;s lifecycle and provides operations for cancellation and completion.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is structured concurrency?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Structured concurrency organizes concurrent operations into parent-child scopes with predictable lifetime and cancellation behavior.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between coroutineScope and supervisorScope?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">coroutineScope normally propagates child failures and cancels sibling operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">supervisorScope allows child failures to remain isolated from siblings, provided those failures are handled appropriately.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is SupervisorJob?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">SupervisorJob is a Job implementation that prevents a child failure from automatically cancelling sibling coroutines.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Does SupervisorJob prevent all coroutine failures?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No. It changes failure propagation but does not automatically catch or suppress exceptions.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What happens when a parent coroutine is cancelled?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Its child coroutines are normally cancelled as well.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What is the difference between viewModelScope and lifecycleScope?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">viewModelScope is associated with a ViewModel.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">lifecycleScope is associated with a LifecycleOwner such as an Activity or Fragment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can I use CoroutineScope in Jetpack Compose?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Compose provides APIs such as LaunchedEffect and rememberCoroutineScope for composition-related coroutine work.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Should I create a new CoroutineScope in every repository?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Usually not.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Repositories should generally expose suspend functions or Flows and let callers manage their lifetimes, unless the repository explicitly owns longer-lived work.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What We&#8217;ve Learned<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In this tutorial, we learned:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CoroutineScope<\/li>\n\n\n\n<li>CoroutineContext<\/li>\n\n\n\n<li>Job lifecycle<\/li>\n\n\n\n<li>Parent-child coroutine relationships<\/li>\n\n\n\n<li>Structured concurrency<\/li>\n\n\n\n<li>Cooperative cancellation<\/li>\n\n\n\n<li>cancelAndJoin<\/li>\n\n\n\n<li>Exception propagation<\/li>\n\n\n\n<li>coroutineScope<\/li>\n\n\n\n<li>supervisorScope<\/li>\n\n\n\n<li>SupervisorJob<\/li>\n\n\n\n<li>launch vs async failure handling<\/li>\n\n\n\n<li>viewModelScope<\/li>\n\n\n\n<li>lifecycleScope<\/li>\n\n\n\n<li>LaunchedEffect<\/li>\n\n\n\n<li>rememberCoroutineScope<\/li>\n\n\n\n<li>Concurrent dashboard loading<\/li>\n\n\n\n<li>StateFlow integration<\/li>\n\n\n\n<li>Jetpack Compose integration<\/li>\n\n\n\n<li>Common mistakes and best practices<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Understanding CoroutineScope, Job, and Structured Concurrency is essential for building production-quality Android applications.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">These concepts help developers manage asynchronous operations safely while avoiding unmanaged tasks, unnecessary work, and unpredictable cancellation behavior.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By combining structured concurrency with ViewModel, StateFlow, and Jetpack Compose, we can create reliable, lifecycle-aware Android applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In our next tutorial, we&#8217;ll explore:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Kotlin Flow Explained for Android: Flow, StateFlow &amp; SharedFlow<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You&#8217;ll learn:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>What Kotlin Flow is<\/li>\n\n\n\n<li>Cold vs hot streams<\/li>\n\n\n\n<li>Flow builders<\/li>\n\n\n\n<li>emit and collect<\/li>\n\n\n\n<li>Flow operators<\/li>\n\n\n\n<li>StateFlow<\/li>\n\n\n\n<li>SharedFlow<\/li>\n\n\n\n<li>StateFlow vs SharedFlow<\/li>\n\n\n\n<li>Flow exception handling<\/li>\n\n\n\n<li>Flow cancellation<\/li>\n\n\n\n<li>Room and Flow integration<\/li>\n\n\n\n<li>ViewModel and StateFlow<\/li>\n\n\n\n<li>Jetpack Compose collection<\/li>\n\n\n\n<li>Practical Android examples<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Happy Coding!<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Continue learning modern Android development with <strong>Moiks Developer Blog<\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Master Kotlin CoroutineScope, Job, structured concurrency, SupervisorJob, cancellation, and lifecycle-aware coroutine management with practical Android and Jetpack Compose examples.<\/p>\n","protected":false},"author":1,"featured_media":152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0,"footnotes":""},"categories":[3,333,7],"tags":[],"class_list":["post-151","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-development","category-android-tutorials","category-coroutines-flow"],"_links":{"self":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":153,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions\/153"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/moiks.com\/blog\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}