Coroutines

What is Coroutines?

Coroutines are used for performing asynchronous task at the background without affecting or blocking the main thread.

Why we use Coroutines for background operation since we have thread and lot more?

Coroutines are light weight thread we can perform asynchronous task on background thread but creating n number of background thread result in out of memory,So we can create single Coroutine and we can perform multiple operation inside it like database access, network operation etc. The Coroutine can communicate with each other and wait.And each Coroutine has its own scope attached to it.

What are the Coroutine builder we have?

async the main purpose is to perform the asynchronous task and wait for the result it return deferred job object and last line of the scope as an return type by using job await we can wait for the result We can perform operation parallely in async.

runBlocking it block the thread from executing until it completes the Coroutine finishes.The main purpose of runblocking is its used in unit test since we cannt call suspend function from another function so we assign the function with runblocking.

launch it doesnt care about the result it will fire and forgot it return deffered job object by using this we can cancel the job.

WithContext it perform serial operation it execute job one by one

What are Scope of Coroutines?

GlobalScope it will live as long as the application exist.

ViewModelScope it live as long as viewmodel exist

Types Of Dispatcher we have in Coroutines?

Dispatcher.IO to perform long running operation like network calls by default it has 64 thread in it so we can do parallel task since it spend lot time on waiting.

Dispatcher.Main to perofrm ui related operation

Dispatcher.Default to perofrm small operation like json parsing since it have only limited thread.

Knowledge is everything.Check out my app at https://play.google.com/store/apps/details?id=com.vibe.britevibe

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

ADB Shell commands for Android

RecyclerView — IndexOutOfBoundsException — Inconsistency detected

Pick Image from Gallery in Jetpack Compose

7 most important tips to Impeccable Android App Development

How to Add the HiHello Widget on Android

Flutter — Bottom Tab Bar animation

Activity, Fragment, ViewModel and the Game of Life-cycle

What’s next with Koin? — 2.2 & 3.0 releases

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pandiyan Mani

Pandiyan Mani

Knowledge is everything.Check out my app at https://play.google.com/store/apps/details?id=com.vibe.britevibe

More from Medium

Using navigation component in ViewModels on Android(MVVM Architecture)

Kotlin Coroutines [Part-1]

Diving deep into Kotlin Coroutines.

Using reflection to patch gaps in current Compose APIs