[英]coroutine scope inside withcontext and only withContext usage
you can see below two methods, the first one using withContext and coroutineScope and second function using only coroutine Scope.您可以看到以下两种方法,第一种使用 withContext 和 coroutineScope,第二种 function 仅使用协程 Scope。 What if it will used only withContext?
如果它只使用 withContext 怎么办? or only coroutine scope?
还是只有协程 scope? Why both need to be used?
为什么两者都需要使用?
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) }
}
}
In that case there is no need for coroutineScope
inside withContext
, since withContext
does this for you already.在这种情况下,
withContext
内不需要coroutineScope
,因为withContext
已经为您完成了。
There are situations where using coroutineScope
inside withContext
could be useful.在某些情况下,在
withContext
中使用coroutineScope
可能会很有用。
withContext(ioDispatcher) {
coroutineScope {
launch { tasksRemoteDataSource.deleteAllTasks() }
launch { tasksLocalDataSource.deleteAllTasks() }
}
coroutineScope {
launch { tasksRemoteDataSource.deleteAllTasks() }
launch { tasksLocalDataSource.deleteAllTasks() }
}
}
Here the coroutineScope
s are use for parallel decomposition and to delineate the set of concurrent tasks.这里
coroutineScope
用于并行分解和描述并发任务集。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.