launchMain
fun CoroutineScope.launchMain(context: CoroutineContext = EmptyCoroutineContext, start: CoroutineStart = CoroutineStart.DEFAULT, block: suspend CoroutineScope.() -> Unit): Job
Content copied to clipboard
Launches a new coroutine without blocking the current thread and returns a reference to the coroutine as a Job. The coroutine is cancelled when the resulting job is cancelled.
Uses the maindispatcher.
The specific dispatcherProvider
instance to be used is determined after the context parameter has been folded into the receiver's context.
The selected DispatcherProvider is essentially the first non-null result from:
the context parameter
the receiver CoroutineScope's coroutineContext
DefaultDispatcherProvider.get Extracts the DispatcherProvider from the CoroutineScope receiver
Samples
import dispatch.core.launchDefault
import dispatch.core.launchIO
import dispatch.core.launchMain
import dispatch.core.launchMainImmediate
import dispatch.core.launchUnconfined
import dispatch.internal.test.Sample5
import dispatch.internal.test.dispatcherName
import dispatch.internal.test.someDispatcherProvider
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.runBlocking
fun main() {
//sampleStart
runBlocking(someDispatcherProvider) {
dispatcherName() shouldBe "runBlocking thread"
launchMain {
dispatcherName() shouldBe "main"
}.join()
}
//sampleEnd
}
See also
launch