What Are Coroutines and When Do We Use Them

Posted By : Pawan Kumar | 30-Oct-2020

ERP

Loading...

Coroutines

Coroutines are the functions or tasks which are run on the background blocking the main thread like file downloading, uploading and playing music as well. We can create a large number of coroutines on the background thread. If we create a large number of worker threads, then the application will crash and that's why instead of worker thread we should use coroutines.

What are coroutine builders?

Coroutines are functions in which we create a coroutines

Different type of coroutine builders

1. Launch func 2. Async func 3. Runblocking func

Launch func

The syntax for the launch function is:

* for the Global scope

GlobalScope.launch{}

Global coroutines are the top level coroutines and can survive the entire life of the application. Even if the all local scope is destroyed then also the global coroutine no matter from which part of the application it was launched, it can survive the entire lifespan of the application.

* for the Local scope

launch{}

Local scope is the coroutines which can survive the entire life cycle of the activity. If the activity is destroyed then the local scope is also destroyed.

When should we use a Global scope and when local scope

*Global Scope

GlobalScope.launch{

// File download

// Play music

}

we can use global scope during the downloading files and playing music etc. *Local Scope

launch{

// Some data Computing

// login operations

}

We can use local scope where we want to compute values and there only.and we can use in login operation because only at the starting of the app the login is required and end at there. we can use the launch function.

2. Async func

Async function is the same as the launch function bs there is a small difference launch function returns a job function and async function will return a defferedjob. for example

var job : Job = launch{

// Some data Computing

// login operations

}

var job : DefferedJob = async{

// Some data Computing

// login operations

}

var job : Job = GlobalScope.launch{

// Some data Computing

// login operations

}

var job : DefferedJob = GlobalScope.async{

// Some data Computing

// login operations

}

Using the deferred object we can the coroutine, wait for the coroutine to finish or retrieve the returned result.

3. Runblocking function

RunBlocking is used to write the test case to test the suspending function for example:

// this is the suspend function

Suspend func myFunction(){

delay(5000)

}

// test the suspend function

@Test

func mytestFunc() = runBlocking {

myFunction()

Assert.assertEquals(10,5+5)

}

// the runblocking func suspend the main thread the run the suspend function

Note: The launch and async function didn't block the main thread but the runBloking function blocks the thread.

We, at Oodles ERP, pride ourselves as a 360-degree ERP development company having vast experience in custom ERP software development. Our development team has years of industry experience in developing custom ERP solutions to address diverse business needs and requirements.