11

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 CommonPool and everything worked correctly.

Now I'm trying to use the GlobalScope in my ViewModel while reading from a room database, and then I want to assign the value returned to my LiveData object.

I'm getting the following error when I try to set the LiveData value

java.lang.IllegalStateException: Cannot invoke setValue on a background thread

fun getContact() {
        GlobalScope.launch {
            val contact = contacts.getContact() // suspended function
            withContext(Dispatchers.Default) { phoneContact.value = contact }
        }
    }

I only see Default, Unconfined and IO for dispatchers, and none of them work, I can't figure out what I'm doing wrong? Where is my option for the Main Thread?

2 Answers 2

40

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.

13
  • I'm running the coroutine from my ViewModel so isn't the global scope okay since the ViewModel isn't tied to the life cycle? For example, if I'm downloading data, the download will continue even if I've left that activity, and when it finishes LiveData will get updated. The next time I return to the activity, it will subscribe to the LiveData and updated accordingly?
    – Ben987654
    Oct 1, 2018 at 15:18
  • Just adding a note - this is my first time using MVVM and LiveData so I could be wrong in what I said above.
    – Ben987654
    Oct 1, 2018 at 16:13
  • Haven't used MVVM so I don't know the details on its threading model. But you shouldn't use GlobalScope in any case. At least you should use a coroutine scope tied to the Application instance and it should specify Dispatchers.MAIN as the default. Without that you're forced to write launch(MAIN) everywhere to start a UI coroutine. Oct 1, 2018 at 17:41
  • I'll keep looking into that further, but using the Application scope sounds like the right way to do it in this case. Thanks for the tips!
    – Ben987654
    Oct 1, 2018 at 17:59
  • 2
    @hippietrail implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.1' implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.1"
    – Mahmoud
    Sep 3, 2019 at 8:16
9

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

1
  • Not for me. I have that in my gradle file already but Dispatchers is still red. Sep 1, 2019 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.