Coroutines

Coroutine Scope in Kotlin | Coroutine Context

In this blog, we’ll talk about coroutine scope. What is scope in the relation to coroutine? How does work and what possible types we can use in Kotlin?

What is coroutine scope

The scope simply provides lifecycle methods for coroutines and allows us to manage coroutines to start and stop them.

GlobalScope.launch { }

Now we have already seen the global scope when we run the hello word code in our previous article. So GlobalScope, simply means the scope of this coroutines is the entire application. It will not get finished, if it is running in the background it won’t stop unless the whole application stop.

The important this is a very large scope and it’s usually not used. we used it in HelloWord program because it’s very simple, very easy. It allows us to create a background thread. It is not very commonly used.

        GlobalScope.launch {
            delay(500L)
            println("Task from GlobalScope")
        }

runBlocking

The next stuff is run blocking, Now run runBlocking creates scope and run coroutine in a blocking way. so this again you would not use very often. You would use only use it when you want to stop the execution of a code and run your coroutines.

In other situations where you might want to use it If this is the only thing that your program does. We will use this quite extensively in our examples to start a scope for our coroutines when this is the only thing that you are trying to do.

    runBlocking {
        launch {
            delay(1000L)
            println("Task from runBlocking")
        }

coroutinesScope { }

Finally, we have coroutinesScope, It creates a new scope and does not complete until all children’s coroutines complete. So we are creating a scope, we are running coroutines and inside the scope, we can create other coroutines. This coroutine that starts here does not complete until all the inner coroutines complete as well.

 coroutineScope {
            launch {
                delay(1500L)
                println("Task from coroutineScope")
            }
        }

Coroutine Context

A context is very tightly related to the scope. So you can say Scope and Context are very similar. They are basically concepts but used in different ways.

A context is a set of data that related to the coroutine. In simple words, Context is a set of variables that you can access inside of the coroutine. All coroutine has an associate context

Scope and Context are very similar only difference is that scope used to create and manage coroutine. Whereas Context is the set of variables and data associate with that coroutine.

Important element of Coroutines Context

  • Dispatcher – It simply decides which thread the coroutine runs on. We’ll learn more about Dispatcher in the upcoming blog.
  • Job – It basically handle the coroutine lifecycle. This concept also I’ll write separate articles on that.

All right, So that is basic idea of a Coroutine Context. Now I’m taking one example of Coroutine Context.

Let go head and create a coroutine and we’ll access a peace of data from that coroutine.

import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking

fun main() {
    runBlocking {
        launch(CoroutineName("myCoroutine")) {
            println("This is run from ${coroutineContext.get(CoroutineName.Key)}")
        }
    }
}

In the above code snippets, Here I passed a function named is CoroutineName(“myCoroutine”). You can pass a name here in our launch. Inside the Coroutine, I simply print some message. Now run the code you will get like below. So here is a coroutine context.

This is run from CoroutineName(myCoroutine)

Summary

Let summarises what we learned in this tutorials

Scope – Provides lifecycle methods for coroutines and allow us to start and stop coroutines

GlobalScope.launch { } – the scope of the coroutine is the lifecycle of the entire application

runBlocking – creates a scope and runs a coroutine in a blocking way

coroutineScope { } – creates a new scope and does not complete until all children coroutines complete

Learn more about our coroutines series

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments