我在其中有一个 OperationQ 和 for 循环。在这个 for 循环中,我调用 dispatch_after 每当 for 循环迭代发生时,延迟就会增加。这是为了确保 for 循环内发生的事情会延迟并按顺序发生。
现在我想用新数据重置这个 OperationQ。我正在取消 operationQ 的所有操作。这会取消操作,但在调度计时器仍在运行的情况下抛出后调度。有什么办法可以取消operationQ里面的dispatch_after吗?
OperationQ addoperationWithBlock({
mainQ addOperationwithBlock({
// Do some UI work
delayCOnstant
for (iteration)
{
delay += delayCOnstant
dipathTime = dispatch_timeNow + delay
dispatch_after(time, mainQ,
{
// UI Drawing, dispatch after will draw smoothly and sequence
})
}
})
})
当我取消 OperationQ 时,操作停止,但 dispatch_after 仍在按照我们给定的 dispatch_time 运行。任何想法或建议
最佳答案
dispatch_after
没有取消机制。能够取消是NSOperationQueue
的优点之一。因此,不要直接在您的 dispatch_after
block 中完成工作,而是使用该 block 来安排 NSOperation
,然后也可以将其取消。
关于ios - 如何使用for循环和dispatch_after取消操作队列并增加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058466/