我正在尝试在DispatchWorkItem
using中添加异步任务列表semaphore
。
func performStickerWorkItems(_ stickers: [String]) {
let queue = DispatchQueue.global(qos: .background)
workItem = DispatchWorkItem { [weak self] in
for sticker in stickers {
guard let item = self?.workItem, !item.isCancelled else {
print("cancelled")
break
}
let semaphore = DispatchSemaphore(value: 0)
self?.addSticker(sticker: sticker) {
print("S: \(sticker)")
semaphore.signal()
}
semaphore.wait()
}
self?.workItem = nil
}
queue.async(execute: workItem!)
workItem?.notify(queue: .main) {
self.updateButton(false)
}
}
如果我回去,我希望它打破循环和过程。为此,我使用该cancel
方法取消DispatchWorkItem
但它不会中断循环。
@IBAction func backWasPressed(_ sender: Any) {
workItem?.cancel()
navigationController?.popViewController(animated: true)
}
有什么解决方案可以打破semaphore
我按下后退按钮时的循环和过程?