Coroutines: First things first

<p>This series of blog posts goes in-depth into cancellation and exceptions in Coroutines. Cancellation is important for avoiding doing more work than needed which can waste memory and battery life; proper exception handling is key to a great user experience. As the foundation for the other 2 parts of the series (<a href="https://medium.com/androiddevelopers/cancellation-in-coroutines-aa6b90163629" rel="noopener">part 2: cancellation</a>,&nbsp;<a href="https://medium.com/androiddevelopers/exceptions-in-coroutines-ce8da1ec060c" rel="noopener">part 3: exceptions</a>), it&rsquo;s important to define some core coroutine concepts such as&nbsp;<code>CoroutineScope</code>,&nbsp;<code>Job</code>&nbsp;and&nbsp;<code>CoroutineContext</code>&nbsp;so that we all are on the same page.</p> <p>If you prefer video, check out this talk from KotlinConf&rsquo;19 by&nbsp;</p> <p><a href="https://medium.com/u/d5885adb1ddf?source=post_page-----e6187bf3bb21--------------------------------" rel="noopener" target="_blank">Florina Muntenescu</a></p> <p>&nbsp;and I:</p> <p><iframe frameborder="0" height="480" scrolling="no" src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2Fw0kfnydnFWI%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dw0kfnydnFWI&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fw0kfnydnFWI%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" title="KotlinConf 2019: Coroutines! Gotta catch 'em all! by Florina Muntenescu &amp; Manuel Vivo" width="854"></iframe></p> <h1>CoroutineScope</h1> <p>A&nbsp;<code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/" rel="noopener ugc nofollow" target="_blank"><strong>CoroutineScope</strong></a></code>&nbsp;keeps track of any coroutine you create using&nbsp;<code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html" rel="noopener ugc nofollow" target="_blank">launch</a></code>&nbsp;or&nbsp;<code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html" rel="noopener ugc nofollow" target="_blank">async</a></code>&nbsp;(these are extension functions on&nbsp;<code>CoroutineScope</code>). The ongoing work (running coroutines) can be canceled by calling&nbsp;<code>scope.cancel()</code>&nbsp;at any point in time.</p> <p>You should create a&nbsp;<code>CoroutineScope</code>&nbsp;whenever you want to start and control the lifecycle of coroutines in a particular layer of your app. In some platforms like Android, there are KTX libraries that already provide a&nbsp;<code>CoroutineScope</code>&nbsp;in certain lifecycle classes such as&nbsp;<code><a href="https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#(androidx.lifecycle.ViewModel).viewModelScope:kotlinx.coroutines.CoroutineScope" rel="noopener ugc nofollow" target="_blank">viewModelScope</a></code>&nbsp;and&nbsp;<code><a href="https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#lifecyclescope" rel="noopener ugc nofollow" target="_blank">lifecycleScope</a></code>.</p> <p>When creating a&nbsp;<code>CoroutineScope</code>&nbsp;it takes a&nbsp;<code>CoroutineContext</code>&nbsp;as a parameter to its constructor. You can create a new scope &amp; coroutine with the following code:</p> <pre> // Job and Dispatcher are combined into a CoroutineContext which // will be discussed shortly val scope = CoroutineScope(Job() + Dispatchers.Main)val job = scope.launch { // new coroutine }</pre> <h1>Job</h1> <p>A&nbsp;<code>Job</code>&nbsp;is a handle to a coroutine. For every coroutine that you create (by&nbsp;<code>launch</code>&nbsp;or&nbsp;<code>async</code>), it returns a&nbsp;<code><a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html" rel="noopener ugc nofollow" target="_blank"><strong>Job</strong></a></code>&nbsp;instance that uniquely identifies the coroutine and manages its lifecycle. As we saw above, you can also pass a&nbsp;<code>Job</code>&nbsp;to a&nbsp;<code>CoroutineScope</code>&nbsp;to keep a handle on its lifecycle.</p> <p><a href="https://medium.com/androiddevelopers/coroutines-first-things-first-e6187bf3bb21">Click Here</a></p>