withIO
suspend fun <T> withIO(context: CoroutineContext = EmptyCoroutineContext, block: suspend CoroutineScope.() -> T): T
Content copied to clipboard
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result.
Uses the iodispatcher.
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.withDefault
import dispatch.core.withIO
import dispatch.core.withMain
import dispatch.core.withMainImmediate
import dispatch.core.withUnconfined
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"
withIO {
dispatcherName() shouldBe "io"
}
}
//sampleEnd
}
See also
withContext