I'm trying to run a task in the background on Android, and I was wondering whether I need to specify GlobalScope.launch(Dispatchers.IO) { ... } or if a simple GlobalScope.launch { ... } is enough. My worry is whether the second form launches the coroutine in the main or a background/IO thread?
According to Android documentation,
launchdoesn't take aDispatchers.IOparameter. When you don't pass aDispatcherto launch, any coroutines launched fromviewModelScoperun in the main thread.
According to Kotlin documentation,
The default dispatcher that is used when coroutines are launched in GlobalScope is represented by Dispatchers.Default and uses a shared background pool of threads, so
launch(Dispatchers.Default) { ... }uses the same dispatcher asGlobalScope.launch { ... }.
I know coroutines were experimental until recently, and Android-Kotlin vs pure-Kotlin development are different, but these statements seem contradictory to me.