KOTLIN COROUTINE: don't click, this post is only for myself
Tags: android, kotlin, beginners, programming
Published: 2021-03-28 09:20:06

Hey there,
If you are reading this post, that would mean that you are already curious about what I am going to talk about Kotlin coroutine in this post. And that's the case, my plan worked! Horay!!

Yes, today in this post, I am going to talk about Kotlin coroutine which has been there for a while but for someone like me who haven't really get to use it so far or even get a heads around the concept itself and just really want to start it off with understanding it in a easy and straightforward way. So if you are already a kotlin coroutine expert, this post might be probably too easy for you then feel free to skip:)

Enough for chit chat I guess, and let's just dive in!

<H2> Kotlin Coroutine: What is it? </H2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1odsd8fi4muuxhsuc8a.png)

Before going straight into the topic, let me ask you something. 
Have you ever been in a frustrating situation where you are out for dinner date and all of a sudden feeling this urge that you really need to go to bathroom? And finally when you get there, you realise there is only one toilet stall and there are two people waiting in the line?
How miserable!

Wait how on earth is this related to today's topic? 
You are about to find out!

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zre3zarkx1ig0ttav4n9.png)

<b>Synchronous</b>
If you could relate to the situation described above. That means, in programming words, you were in "Synchronous" situation. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7v7x77979jqycyx3lifc.png)

a job(or task) which is, in our case, using toilet can be completed one by one even if there are multiple people(tasks) waiting and we all know it can be quite annoying sometimes. 

<b> Asynchronous</b>

But let's imagine what if there were a few stalls we could use! Then we wouldn't have had to wait for other people to come out. That case is what we call <i>Asynchronous</i>. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i30ka346bsjotkimkdsw.png)
As you can see, in Asynchronous, multiple tasks can be executing at the same time and also other tasks don't need to wait another task that executed before to be finished. 

<H3>Okay, I think it is enough for toilet example. 
Let's talk some real coding example</H3>

```
fun getNetWorkData() {
    val requestRes = networkRequest()
    displayResponse(requestRes)
}

fun networkRequest(): Response {
    // doing some heavy lifting call.
}
```
Let's say there are two functions as you can see above. The first one "getNetworkData" is getting request response from networkRequest() call and displaying it on the screen. And let's just say that networkRequest() function is doing some heavy job probably making network call or some disk operations. 

The issue that we are facing here is that until the networkRequest() is completed, the other task print is blocked to be executed as this function is running on a main Thread just like we had only one toilet stall earlier(sorry for using this example again:( ). 

If this networkRequest() is taking too long, then in real programming world, the UI might be getting laggy or even freeze for a second while waiting <b>which would be very unpleasant to see as users</b>

In an ideal world, we would want to move this non-UI related task <b>to a different thread</b> so that the UI related tasks can be unblocked. 

<b>This is where "Kotlin Coroutine" comes in!</b>

<H2> Kotlin Coroutine: Concepts </H2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/083l474irij3d24mpci7.png)

<H4>With that being said, let's modify the snippet earlier to coroutine way</H4>

```
suspend fun getNetWorkData() {
    val requestRes = networkRequest()
    displayResponse(requestRes)
}

suspend fun networkRequest(): Response {
    withContext(Dispatcher.IO) {
    // doing some heavy lifting call.
    }
}
```
Compare to how it looked previously, there were two modifications made. 
1. suspend
2. withContext

These are the coroutine concepts we will cover in the later post. So please bear with me and let me just briefly explain what that block of code doing. It is pretty much when the networkRequest is running, making the network call to be made in the Thread called <i>Dispatcher.IO</i> rather than the same Thread that this UI is running to avoid blocked tasks. 

Wait, Dispatcher.IO.. is there more?

<H4>Yes</H4>

<H2> Dispatcher </H2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fodorzw1d9umeyf4ffz.png)

This will mean that depending on the type of task that you want to run behind the scene, you can wisely choose the dispatcher type according to this. 

<H2> Coroutine Scopes </H2>

Earlier, we have talked about "where" we would want to run our task and how to make that happen. 

Here we will talk about "When".
What do you mean? 
Well, let's imagine we have a task that printing the text "running..." in the log every 1 second. Depending on the purposes, we might want to run this task while the app is active. For example, this text will be displayed from the start of an app and finished when the app is killed. Or we might also want to run the task for a particular time of an app and make the task is cancelled when it is no longer needed. "When" do we want this task and "When" do we not want this task to be run can be defined with "Coroutine Scope".

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bez7pbr7t84pzg68qd3.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xw2bggmww0ivtqxp4bz2.png)

So let me put  it this way, the method that I described above that prints "running.." in the log, this will be happening as long as the app is running if we use globalScope. 

```
    GlobalScope.launch {
       delay(1000)
       Log.d("coroutine", "running..")
    }
```

For lifecycleScope,
```
    lifecycleScope.launch {
       delay(1000)
       Log.d("coroutine", "running..")
       // Any coroutine launched in this scope is canceled when the Lifecycle is destroyed. 
    }
```

For viewModelScope,
```
    viewModelScope.launch {
       delay(1000)
       Log.d("coroutine", "running..")
       // Coroutine that will be canceled when the ViewModel 
      is cleared.
    }
```


<H2> We will dive in deeper in the next post! </H2>

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kfy4t4cqag8ijq1601mb.jpeg)

Alright, in this post, we have covered the basic concept of Kotlin coroutine on what it is, why we need it, How we make sure when and where tasks are executed. 

In the second post, we will cover how we define the task in coroutine and how we make it run and how we make it finished with some real code examples. Hope now you guys have at least clear idea of what Kotlin coroutine is. And for more details in codes, don't worry we will get there once we have the second post! 

Links for references
<a href="https://developer.android.com/topic/libraries/architecture/coroutines">uptrends.com/tools/website-speed-test</a>

<a href="https://kotlinlang.org/docs/coroutines-overview.html">https://kotlinlang.org/docs/coroutines-overview.html</a>

<a href="https://www.youtube.com/watch?v=C38lG2wraoo">https://www.youtube.com/watch?v=C38lG2wraoo</a>