如何停止现有的调度队列并开始新的?
Posted
技术标签:
【中文标题】如何停止现有的调度队列并开始新的?【英文标题】:How to stop existing dispatchqueue and start new? 【发布时间】:2018-10-04 06:17:58 【问题描述】: @objc func textFieldChanged(_ textField: UITextField)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute:
self.shouldEnableBtn()
)
在这里,如果我再次输入 textFieldChanged,我想取消现有的 dispatchque 并重新开始。
【问题讨论】:
你可以试试DispatchWorkItem
developer.apple.com/documentation/dispatch/dispatchworkitem
【参考方案1】:
您可以使用DispatchWorkItem
类,它允许您单独取消您的任务。
let workItem = DispatchWorkItem
self.shouldEnableBtn()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: workItem)
// To cancel the work-item task
workItem.cancel()
您最好使用OperationQueue
来完成此任务,如下所示:
let operationQueue = OperationQueue()
operationQueue.maxConcurrentOperationCount = 1
// Add operation in the queue
operationQueue.addOperation
self.shouldEnableBtn()
// Cancel to on-going operation by
operationQueue.cancelAllOperations()
// Pause to on-going operation by
operationQueue.isSuspended = true
【讨论】:
谢谢,我不能使用操作队列,因为它包含 UI。对于第一个示例,我需要先取消现有的,然后再创建新的。以上是关于如何停止现有的调度队列并开始新的?的主要内容,如果未能解决你的问题,请参考以下文章
如果现有的屏幕交互经过并在 UIView 上停止,则运行操作