Snippet from the above Code link:
override suspend fun activateTask(taskId: String) {
withContext(ioDispatcher) {
(getTaskWithId(taskId) as? Success)?.let { it ->
activateTask(it.data)
}
}
}
override suspend fun clearCompletedTasks() {
coroutineScope {
launch { tasksRemoteDataSource.clearCompletedTasks() }
launch { tasksLocalDataSource.clearCompletedTasks() }
}
}
override suspend fun deleteAllTasks() {
withContext(ioDispatcher) {
coroutineScope {
launch { tasksRemoteDataSource.deleteAllTasks() }
launch { tasksLocalDataSource.deleteAllTasks() }
}
}
}
override suspend fun deleteTask(taskId: String) {
coroutineScope {
launch { tasksRemoteDataSource.deleteTask(taskId) }
launch { tasksLocalDataSource.deleteTask(taskId) }
}
}
When to use which one?
Sometimes the coroutineScope { launch { code } }
is inside withContext(iODispatcher)
!
When to use: coroutineScope { launch { code } }
When to use: withContext(iODispatcher)
When to use them nested: coroutineScope { launch { code } }
is inside withContext(iODispatcher)
withContext(ioDispatcher)
should be used to offload blocking IO tasks running inside{}
to a shared pool of threads.coroutineScope { }
creates a child scope, that inherits the coroutineContext from the parent scope's coroutine context. It should be used for "parallel decomposition of work". Also this means when any of the child coroutines in the scope fails the scope and any other child scope will be canceled as well