[英]Swift wait for async task
在后台线程中,我们有:
defer {
cleanup()
}
loadData()
if error {
return
}
processData()
DispatchQueue.main.asyncAfter(deadline: delay) { //Delay = now + 0-2 seconds
updateUI()
}
问题是我们要确保 defer cleanUp()
代码在updateUI()
之后运行。 就目前而言,这不会发生,因为updateUI()
异步运行。
我的想法是在延迟期间休眠/阻塞,而不是异步运行。 一旦updateUI()
完成,这将使 defer cleanUp()
运行。
你怎么能做到这一点? 或者,还有更好的方法?
您可以使用信号量告诉清理任务等待updateUI
完成:
let semaphore = DispatchSemaphore(value: 1)
defer {
semaphore.wait()
cleanup()
}
loadData()
if error {
// If we exit here, the semaphore would have never been used
// and cleanup will run immediately
return
}
processData()
semaphore.wait() // here, we claim the semaphore, making cleanup
// wait until updateUI is done
DispatchQueue.main.asyncAfter(deadline: delay) {
updateUI()
semaphore.signal()
}
意识到我可以改变代码的结构:
loadData()
if error {
log.error("Error")
} else {
processData()
}
DispatchQueue.main.asyncAfter(deadline: delay) { //Delay = now + 0-2 seconds
updateUI()
cleanup()
}
另一种选择是使用DispatchGroup()
:
func doWork() {
let group = DispatchGroup()
group.enter() //Enter #1
loadData { result in
switch (result) {
case .success(_):
group.enter()//Enter #2
processData { group.leave()//Leave #2 }
case .failure(let error):
//Do something nice with the error
print(error)
}
group.leave()//Leave #1
}
//All the code inside this block will be executed on the mainThread when all tasks will be finished.
group.notify(queue: .main) { [weak self] in
guard let strongSelf = self else { return }
strongSelf.updateUI()
strongSelf.cleanup()
}
}
private func updateUI() {
//All your stuff
}
private func cleanup() {
//All your stuff
}
private func loadData(completion: (Result<(), Error>) -> ()) {
//All your stuff
if error {
completion(.failure(error))
}
else {
completion(.success(()))
}
}
private func processData(completion: () -> ()) {
//All your stuff
completion()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.