How to use coroutines GlobalScope on the main thread?

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


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:

class MyActivity : 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.
    override val coroutineContext: CoroutineContext =
            Dispatchers.Main + SupervisorJob()

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

    // this.launch picks up coroutineContext for its context:
    fun loadDataFromUI() = 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.