Understanding — Kotlin Coroutines [Part — 1]
In this article we are going to understand the concept of Kotlin Coroutines what are Coroutines , how we can use it, what are the syntax we can use for same.
Before starting to understand Coroutines lets first understand what is Program. So programs are basically a set of instructions require to achieve a task.
- Now if we run this program there are set of process which followed which gets executes.
- The process provide environment which has ID, States, Memory and Network handling steps.
- These process gets execute inside a thread which is known as Thread Execution. Every process has at least one thread and these thread perform I/O operations.
A thread is a single sequential flow of control within a program.
- So if we run an application it will create a process this process has a thread inside which all code gets execute which you can say as Main Thread or UI Thread.
- Before Coroutines we were executing all task in background Thread like network call or database operations but there could be possibility you have restriction in number of amount thread you can create this is drawback which gets overcomes with the help of Coroutines.
What are Kotlin Coroutines
- Kotlin Coroutines are work similar as Thread these are not Thread exactly they also known as Light weight threads because they mimic the behavior of Threads.
- They work over Thread like there is a framework above Thread and we interact with that framework and this framework behind the scene interact with threads.
Coroutines run on top of Threads.
How to use Kotlin Coroutines
- Now to define Coroutines we need 2 things:
- Coroutine Scope
- Coroutine Context
Coroutine Scope
- This scope defines the lifetime of Coroutines.
- This scope defines a boundary for you, inside this boundary the Coroutines will get launch.
- The benefit of scope is every component in our application has its own lifecycle if one component gets destroy its own scope other will not get impacted.
- It will provide us a way in which we can use, create or destroy Coroutines inside a particular scope.
Coroutine Context
- This context on which threads our coroutines will get launch.
- This will provide us information on which thread our coroutine will get executes.
Dispatchers
- Dispatchers is a way to define threads on which Coroutines are executed.
- It means we are dispatching Coroutines over thread due to which it gets execute.
- There are few predefine Dispatchers:
1 . Dispatchers.IO — for IO Operations
2. Dispatchers.Main — on Main thread we need to launch our Coroutines
3. Dispatchers.Default
- These are basically a thread pool in which Coroutines gets execute.
- Now on code we need to add dependencies to start with Couroutines.
- Now once inside code you start writing CoroutineScope it will ask you to add context and you have to add Dispatchers and it will give us a lunch it will help us to launch the coroutines and log the thread information.
- Similar to Dispatchers there are predefine scopes which are attached to particular components.
- 1. GlobalScope — Global Scope is one of the ways by which coroutines are launched. When Coroutines are launched within the global scope, they live long as the application does. If the coroutines finish it’s a job, it will be destroyed and will not keep alive until the application die.
- 2 . LifecycleScope— The lifecycle scope is the same as the global scope, but the only difference is that when we use the lifecycle scope, all the coroutines launched within the activity also dies when the activity dies.
- 3. ViewModelScope — It is also the same as the lifecycle scope, only difference is that the coroutine in this scope will live as long the view model is alive. ViewModel is a class that manages and stores the UI-related data by following the principles of the lifecycle system.
- We will go through in deep regarding above 2 scope in our next blog and will focus more on launch ,suspend and async functions.
Summary
Coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.
References
Android doc — https://developer.android.com/kotlin/coroutines/coroutines-adv
Yes..that’s all :)
We will come again with part 2 with more detail with application components.
Happy Reading:)