Handling Kotlin Coroutines — Understanding Jobs and coroutineScope
When I see words like threads and suspended functions they intrigue me. They sound complex but are just simple concepts. The same thing happened to me when I heard about Kotlin Coroutines. Kotlin coroutines sound complex but are a simple concept if you understand them.
I have previously written about Kotlin coroutines. This will be an extension of the existing one. I would suggest that you read the previous one first.
Let’s explore more about Kotlin coroutines.
We will explore:
- CoroutineScope
- Jobs
If you are trying to call a suspended function from a composable function, you must have encountered coroutine scope before.
Coroutine Scope
At the heart of managing coroutines lies CoroutineScope. It serves as a container that allows you to execute coroutines at a specific scope. Think of it as a supervisor that keeps track of the coroutines within its scope. With CoroutineScope, you gain control over when and where your coroutines should be executed.
When working with CoroutineScope, you can create coroutines using two fundamental functions: launch
and async
. These functions initiate suspended functions, defining the tasks that your coroutines will perform. CoroutineScope acts as the overseer, ensuring these functions are executed according to your specifications.
Two prominent types include:
- ViewModel Scope: Scoped to the lifespan of a ViewModel, these coroutines help manage asynchronous tasks tied to UI components.
- Lifecycle Scope: Aligned with the Android Lifecycle, this scope ensures coroutines are aware of and adapt to the state of the app’s components.
- It helps us to call Kotlin Coroutine functions.
- It has async and Launch functions which are used to create Kotlin coroutines
- There are many types of scopes:
— > ViewModel Scope
— > Lifecycle Scope
— > Runblocking
— > Global Scope
Global Scope → The life cycle of this component is tied to the lifecycle of the whole application
Lifecycle Scope → Aligned with the Android Lifecycle
ViewModel Scope → Scoped to the lifespan of a ViewModel
RunBlocking → We can use this scope to create Kotlin coroutines wherever we want, but it is not a recommended approach
Jobs
Why should you learn about Jobs:
- They provide a structured way to track and understand the progress of asynchronous tasks
- Jobs can cancel Kotlin coroutines.
- They take charge of when a coroutine begins, progresses, and concludes, and provide control over the execution flow.
What are Jobs?
- As the name suggests Jobs are like tasks assigned to Kotlin coroutines
- For example — A job of a Network call
- Whenever a Kotlin Coroutine is called a job is automatically created
We can also store a job in a variable like this:
Jobs manage the lifecycle of Kotlin Coroutines:
There are two essential functions in Jobs in Kotlin Coroutines:
- Job. join()
- Job.cancel()
Job. join() — It blocks the main thread until the job is completed. By invoking join()
, the main thread patiently waits until the associated job concludes its execution, ensuring a smooth and controlled flow of your application.
Job. cancel() — It cancels the job once called. The cancel()
function provides developers with the ability to halt the execution of a coroutine
We can check the life cycle of Kotlin Coroutines like this:
When we do a network call, we use a timeout function to stop the network calling after a time. We can use a similar timeout function to cancel a job after a time: