gpt4 book ai didi

ios - 如何停止现有的调度队列并开始新的?

转载 作者:行者123 更新时间:2023-11-28 05:51:55 24 4
gpt4 key购买 nike

    @objc func textFieldChanged(_ textField: UITextField) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1, execute: {
self.shouldEnableBtn()
})
}

在这里,如果我再次输入 textFieldChanged,我想取消现有的 dispatchque 并开始新的。

最佳答案

您可以使用 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

关于ios - 如何停止现有的调度队列并开始新的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52640209/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com