Load data in IO pool and print in UI thread

Report a typo

Let's replicate a very common patten in UI applications: load some data, wait for it to enter the IO thread without blocking the UI, then display it in the UI thread, and let the IO code do its job.

There is no Dispatchers.Main available for our console app, so use UiDispatcher instead.

Tip: launch() can take any context element as a parameter, so use it to set the right dispatcher.

This task uses dependencies that may not be available in your local IDE. As a result, you may see compilation errors or unresolved references when running the code locally. This does not affect solution validation when you submit your solution to Hyperskill.

Write a program in Kotlin
fun main() = runBlocking(Dispatchers.Default) {
// Modify this code to make sure data loading and storing
// happens in Dispatchers.IO, but data printing in `UiDispatcher`
// which is already defined for you as a replacement for Dispatchers.Main
val mainJob = mainScope.launch {
val data = loadData()
printData(data)
storeData(data)
}
mainJob.join()
}

Create a free account to access the full topic