Android: (Part 2) Real work of Coroutines.

Vinod Baste
CodeX
Published in
3 min readApr 3, 2022
source: google image

This is part of a multi-part article on using Android Coroutines. One-shot requests are used in this to solve practical difficulties with coroutines.

If you’re new to coroutines, I recommend starting with part 1 of the series.

This series includes the following articles:

Let's get started.

Code Set-Up

You must first add the dependencies to your Android project before you can construct coroutines. Begin by editing the build.gradle file for the app level and adding the following dependencies:

Using Dispatchers With Coroutines Scope

A CoroutineScope keeps track of any coroutine it generates with the launch or async builder functions.

Dispatchers are a way to define threads on which Coroutines are executed.

In the above code, we have used 3 different dispatchers with 3 different scopes of coroutines.

  • CoroutineScope(Dispatchers.I0): The CoroutineScope(Dispatchers.I0) is attached with the input/output operations and it runs over the IO thread.
  • Globalscope.Launch(Dispatchers.Main): The Globalscope.Launch(Dispatchers.Main) is attached with the entire application.
  • Mainscope().launch(Dispatchers.Default): The Mainscope().launch(Dispatchers.Default) is attached with activity it is implemented in.

Output:

With respect to the threads dispatched. We have the output as

Use of Suspend with functions

Coroutines help to implement functionality that can be suspended & later resumed at specified points without blocking the thread.

In the above code snippet, we have defined two functions which are suspending functions.

task1 will execute and suspend until 1 sec then task 2 will execute and suspend for 2 secs.

To launch these suspending functions we have used CoroutineScope.

Output:

You can see that first task 1 is executed and suspended then the coroutine launches the other suspend function task 2 and it suspends to wait until any other suspend functions are done with instructions.

Note: delay and yield are two suspension points that are used in coroutines.

Use of Job

A Job is a cancellable thing with a life cycle that culminates in its completion.

Let’s look at how the job is returned and how it might be used programmatically.

  • job.jion(): within the runBlocking call the join method is called. This means the log is called as soon as the suspend method finishes executing.
  • job.cancel(): is used to stop the coroutine without having to wait for it to complete its task.

the job is canceled after the 2 seconds of the coroutine is launched.

Canceling a coroutine isn’t always as simple as it appears in the example above. It’s important to remember that when using the cancel() method, the coroutine should be aware that the cancel method will be called, as it’s possible that the cancel method will be called while the coroutine is still running. In other words, there must be sufficient time to inform the coroutine that it has been terminated.

To avoid such situations we have to check manually if the coroutine has been canceled or not. This can be done using isActive to check whether the coroutine is active or not.

Once the job is canceled. the coroutine launch is immediately canceled.

  • withTimeOut(): When a specified amount of time has passed, the coroutine will automatically be canceled.

After 3 Seconds of the coroutine launch, the job is canceled.

Thank you for taking the time to read this article. If you found this post to be useful and interesting, please clap and recommend it.

if I got something wrong, mention it in the comments. I would love to improve.

--

--