Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin coroutine - how to ensure some commands run on UI main thread when invoked inside coroutine?

i have a very simple coroutine that just does some delay and then what i want it to do is post commands to UI message queue. so run the last two lines on UI thread. here is the coroutine:

async{
    delay(5000)
    doSomething()
    doAnotherThing()
}

I want the last two methods doSomething() and doAnotherThing() to run on the UI thread ? how can this be done ? From what i have read the delay(5000) will automatically run asynchronously but how to make the rest run on UI thread ? To be very clear, i am doing this from an object that was launched from main thread.

like image 415
j2emanue Avatar asked Feb 23 '19 12:02

j2emanue


People also ask

Is coroutine run on main thread?

Use of coroutines in long-running tasksIt does not block the main thread in this process, the main thread is free to do its tasks whatever it is doing. It looks like the sequential code that we have written but it will work without blocking the main thread.

Does coroutine block main thread?

On Android, coroutines help to manage long-running tasks that might otherwise block the main thread and cause your app to become unresponsive.

What determines which thread a coroutine will use for its execution?

Use coroutines for main-safety. Kotlin coroutines use dispatchers to determine which threads are used for coroutine execution. To run code outside of the main thread, you can tell Kotlin coroutines to perform work on either the Default or IO dispatcher.

Why are coroutines lightweight?

Coroutines stick to a thread, and as soon as suspension point is reached, it leaves the Thread and frees it up letting it to pick up another coroutine if it is waiting. This way with less threads and less memory usage, that much concurrent work can be done.


1 Answers

async creates a coroutine and runs in the Coroutine context, inherited from a CoroutineScope, additional context elements can be specified with context argument. If the context does not have any dispatcher nor any other ContinuationInterceptor, then Dispatchers.Default is used.

If Dispatchers.Default is used then whatever function you invoke in async builder it will run asynchronously. To switch contexts you can use withContext function:

async {
    delay(5000)
    withContext(Dispatchers.Main) {
        // if we use `Dispatchers.Main` as a coroutine context next two lines will be executed on UI thread.
        doSomething()
        doAnotherThing()
    }
}

If async runs in the Dispatchers.Main context you don't need to switch contexts:

var job: Job = Job()
var scope = CoroutineScope(Dispatchers.Main + job)

scope.async {
    delay(5000) // suspends the coroutine without blocking UI thread

    // runs on UI thread
    doSomething() 
    doAnotherThing()
}

Note: async is mainly used for parallel executions. To start a simple coroutine launch builder is used. So you can replace all async functions in those examples on launch function. Also to run coroutine with async builder you need to call await() function on Deferred object which is returned by async function. Here is some additional info.

like image 97
Sergey Avatar answered Sep 22 '22 18:09

Sergey