Complete the code to launch a coroutine on the main thread.
CoroutineScope(Dispatchers.[1]).launch {
// UI update code here
}Dispatchers.Main is used to run coroutines on the main thread, which is necessary for UI updates.
Complete the code to perform a network request on the appropriate dispatcher.
CoroutineScope(Dispatchers.[1]).launch {
val data = fetchData()
// process data
}Dispatchers.IO is optimized for blocking IO tasks like network or disk operations.
Fix the error in the code to run a CPU-intensive task on the correct dispatcher.
CoroutineScope(Dispatchers.[1]).launch {
val result = heavyComputation()
// use result
}Dispatchers.Default is optimized for CPU-intensive tasks like heavy computations.
Fill both blanks to create a coroutine that switches from IO to Main dispatcher.
CoroutineScope(Dispatchers.[1]).launch { val data = fetchData() withContext(Dispatchers.[2]) { updateUI(data) } }
Start on Dispatchers.IO for background work, then switch to Dispatchers.Main to update UI.
Fill all three blanks to create a coroutine that performs CPU work, then IO, then updates UI.
CoroutineScope(Dispatchers.[1]).launch { val cpuResult = heavyComputation() val ioResult = withContext(Dispatchers.[2]) { fetchData() } withContext(Dispatchers.[3]) { updateUI(cpuResult, ioResult) } }
Use Default for CPU work, switch to IO for network, then Main for UI updates.