
Structured Concurrency in Kotlin - coroutineScope, Job & Cancellation
Structured Concurrency in Kotlin - coroutineScope, Job & Cancellation Structured concurrency ensures all coroutines are properly tracked and cancelled. Master Job, coroutineScope, and cancellation. Scoped Coroutines coroutineScope creates a scope that waits for all children: suspend fun fetchUserData (): UserData = coroutineScope { val userDeferred = async { fetchUser () } val postsDeferred = async { fetchPosts () } // Both requests run in parallel, but scope waits for both UserData ( user = userDeferred . await (), posts = postsDeferred . await () ) } // Exceptions in either async are propagated Job and Cancellation val job = viewModelScope . launch { try { repeat ( 100 ) { i -> println ( "Working $i" ) delay ( 1000 ) } } catch ( e : CancellationException ) { println ( "Job cancelled" ) throw e // Re-throw to propagate } finally { println ( "Cleanup" ) } } // Cancel the job job . cancel () SupervisorJob for Independent Children val supervisor = SupervisorJob () val scope = CoroutineSc
Continue reading on Dev.to Tutorial
Opens in a new tab




