64.10 Using withContext
As we have seen, coroutines are launched within a specified scope and using a specific dispatcher. By default, any child coroutines will inherit the same dispatcher as that used by the parent. Consider the following code designed to call multiple functions from within a suspend function:
fun startTask(view: View) {
coroutineScope.launch(Dispatchers.Main) {
performTasks()
}
}
suspend fun performTasks() {
performTask1()
performTask2()
performTask3()
}
suspend fun performTask1() {
Log.i(TAG, "Task 1 ${Thread.currentThread().name}")
}
suspend fun performTask2() {
Log.i(TAG, "Task 2 ${Thread.currentThread().name}")
}
suspend fun performTask3 () {
Log.i(TAG...