Skip to content Skip to sidebar Skip to footer

How To Use Coroutines Globalscope On The Main Thread?

I'm trying to use the latest coroutines in 0.30.0, and having trouble figuring out how to use the new scoping. In the original coroutines I could set the context with UI or Common

Solution 1:

You solved your immediate problem by adding the dependency, but let me add a note on your usage of GlobalScope.

Using the GlobalScope in production code is an antipattern. It's there for similar reasons like runBlocking, to make it easy to do quick experiments. You should especially avoid it on Android due to the complicated lifecycle of app components.

If you're launching a coroutine from an Android event handler, you should use the current Activity as its coroutine scope. This will ensure your coroutine gets canceled when the activity gets destroyed. Without that the coroutine will go on, referring to the now-dead activity.

Here's a sample adapted from the documentation on CoroutineScope, it shows how to use your activity as the coroutine scope:

classMyActivity : AppCompatActivity(), CoroutineScope {
    // Sets up the default dispatcher and the root job that we can use to centrally// cancel all coroutines. We use SupervisorJob to avoid spreading the failure// of one coroutine to all others.overrideval coroutineContext: CoroutineContext =
            Dispatchers.Main + SupervisorJob()

    overridefunonDestroy() {
        super.onDestroy()
        coroutineContext[Job]!!.cancel()
    }

    // this.launch picks up coroutineContext for its context:funloadDataFromUI() = this.launch {
        // Switch to the IO dispatcher to perform blocking IO:val ioData = withContext(Dispatchers.IO) {
            // blocking I/O operations
        }
        draw(ioData) // use the data from IO to update UI in the main thread
    }
}

If you're using a ViewModel, use it as the scope and cancel the master job from onClear.

If you're doing work from a background job, use your JobService implementation as the scope and use onStartJob and onStopJob the way we use onCreate and onDestroy above.

Solution 2:

I was missing the Android portion of coroutines in my gradle file

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:0.30.0"

Once I had that, Dispatchers.Main appeared

Post a Comment for "How To Use Coroutines Globalscope On The Main Thread?"