【发布时间】:2021-08-26 05:15:59
【问题描述】:
来自上述代码链接的片段:
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) }
}
}
什么时候用哪个?
有时coroutineScope { launch { code } } 在withContext(iODispatcher) 内!
何时使用:coroutineScope { launch { code } }
何时使用:withContext(iODispatcher)
何时使用它们嵌套:coroutineScope { launch { code } } 在 withContext(iODispatcher) 内
【问题讨论】:
标签: android kotlin kotlin-coroutines coroutinescope