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>, <a href="https://medium.com/androiddevelopers/exceptions-in-coroutines-ce8da1ec060c" rel="noopener">part 3: exceptions</a>), it’s important to define some core coroutine concepts such as <code>CoroutineScope</code>, <code>Job</code> and <code>CoroutineContext</code> so that we all are on the same page.</p>
<p>If you prefer video, check out this talk from KotlinConf’19 by </p>
<p><a href="https://medium.com/u/d5885adb1ddf?source=post_page-----e6187bf3bb21--------------------------------" rel="noopener" target="_blank">Florina Muntenescu</a></p>
<p> 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&display_name=YouTube&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dw0kfnydnFWI&image=https%3A%2F%2Fi.ytimg.com%2Fvi%2Fw0kfnydnFWI%2Fhqdefault.jpg&key=a19fcc184b9711e1b4764040d3dc5c07&type=text%2Fhtml&schema=youtube" title="KotlinConf 2019: Coroutines! Gotta catch 'em all! by Florina Muntenescu & Manuel Vivo" width="854"></iframe></p>
<h1>CoroutineScope</h1>
<p>A <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> keeps track of any coroutine you create using <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> or <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> (these are extension functions on <code>CoroutineScope</code>). The ongoing work (running coroutines) can be canceled by calling <code>scope.cancel()</code> at any point in time.</p>
<p>You should create a <code>CoroutineScope</code> 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 <code>CoroutineScope</code> in certain lifecycle classes such as <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> and <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 <code>CoroutineScope</code> it takes a <code>CoroutineContext</code> as a parameter to its constructor. You can create a new scope & 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 <code>Job</code> is a handle to a coroutine. For every coroutine that you create (by <code>launch</code> or <code>async</code>), it returns a <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> instance that uniquely identifies the coroutine and manages its lifecycle. As we saw above, you can also pass a <code>Job</code> to a <code>CoroutineScope</code> to keep a handle on its lifecycle.</p>
<p><a href="https://medium.com/androiddevelopers/coroutines-first-things-first-e6187bf3bb21">Click Here</a></p>