Hello, I have trouble with `Dispatchers.Main` on A...
# coroutines
k
Hello, I have trouble with
Dispatchers.Main
on Android. Here are my dependencies:
Copy code
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1")
And on my activity, a simple launch would do nothing when ran on
Dispatchers.Main
, whereas it works on `Dispatchers.IO`:
Copy code
GlobalScope.launch(Dispatchers.Main) { // Works with <http://Dispatchers.IO|Dispatchers.IO>
            Timber.d("Current thread: ${Thread.currentThread().name}")
        }
In debug, I can access
Dispatchers.Main
which is correctly plugged to the android handler. Any idea what I'm doing wrong?
t
There should be nothing wrong with
Dispatchers.Main
on Android. I'd advocate against using
GlobalScope
, as it doesn't account for structured concurrency. Try to launch your coroutine from the scope of an `Activity`/`Fragment`/`ViewModel`