Proper use of coroutines Dispatcher Main and Default

ir2pid

I'm trying to do a cup heavy calculation and then want to update the UI.

Below is my code:

private fun updateData() {
        GlobalScope.launch(Dispatchers.Default){ //work on default thread
            while (true){
                response.forEach {
                val out = doIntensiveWork()
                    withContext(Dispatchers.Main){ //update on main thread
                        _data.postValue(out)
                        delay(1500L)
                    }
                }
            }
        }
}

is this way of using coroutines okay? As running the entire work on Main also has no visible effect and work fine.

private fun updateData() {
        GlobalScope.launch(Dispatchers.Main){ //work on Main thread
            while (true){
                response.forEach {
                val out = doIntensiveWork()
                    _data.postValue(out)
                    delay(1500L)
                }
            }
        }
}

Which one is recommended?

ChristianB

You should avoid using GlobalScope for the reason described here and here.

And you should consider doing heavy computation off the main thread


suspen fun doHeavyStuff(): Result = withContext(Dispatchers.IO) { // or Dispatchers.Default
 // ...
}

suspend fun waitForHeavyStuf() = withContext(Dispatchers.Main) {
  val result = doHeavyStuff() // runs on IO thread, but results comes back on Main thread
  updateYourUI()
}

Documentation

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Is this a proper usage of Coroutines IO dispatcher in ViewModel?

What's the proper way to use Coroutines in Activity?

Is it necessary to use IO dispatcher for Firestore calls in Kotlin Coroutines?

Is it good idea to use Dispatcher.Main with delay( )?

Kotlin Coroutines choosing Dispatcher

How to use Kotlin coroutines await() on main thread

How to use coroutines GlobalScope on the main thread?

kotlin coroutines - use main thread in run blocking

Why running coroutine on Dispatcher.Main and Dispatcher.Default returns same result?

Kotlin Coroutines Correct Dispatcher Usage

Coroutines: Overriding OKHttp's dispatcher to use AsyncTasks's ThreadPoolExecutor so Espresso can assert successfully

Which is better to use for API calls Dispatcher.IO or Dispatcher.Main?

What is the proper way to use Dispatcher.RunAsync() to start a task and return immediately?

Module with Main dispatcher is missing

Kotlin Coroutines validate running on same Dispatcher

onEach changes the dispatcher in StateFlow (kotlin coroutines)

Default dispatcher for a custom scope

What is the difference between Dispatchers.Main and Dispatchers.Default in Kotlin coroutines?

CMake does not use CMAKE_CXX_COMPILER as proper default

What is the proper way to cancel coroutines with common mutex

Kotlin coroutines not running launch on main

What is the proper declaration of main?

Coroutines: main() runBlocking vs suspend main

Kotlin coroutines multithread dispatcher and thread-safety for local variables

Switch from Main dispatcher to IO dispatcher from non lifecycle class

Coroutine, No Android, Module with the Main dispatcher is missing

Perform network task in context of Dispatcher.Main

What's the proper way to use variables defined in the main thread in a child thread in Rust?

Proper use of <use> and <svg>