ioDispatcher

expect val ioDispatcher: CoroutineDispatcher

The dispatcher for blocking calls (file reads, sockets, JDBC) from common code.

Dispatchers.IO exists only on some targets. Use ioDispatcher in commonMain wherever you would write Dispatchers.IO on the JVM.

Example

val bytes = withContext(ioDispatcher) { readFileBlocking(path) }

Platform differences

TargetioDispatcher is
JVM, Android, iOS, macOSDispatchers.IO, a thread pool that grows on demand
JS, Wasmthe dispatcher from installIoDispatcher, else Dispatchers.Default

Watch out

  • Browsers and Node have no thread pool. Without installIoDispatcher, JS and Wasm fall back to Dispatchers.Default, which is the main thread. The first read then logs one console warning.

  • Every read returns the currently installed dispatcher. A value that you stored before installIoDispatcher keeps the old dispatcher.

See also