如何使用for循环和dispatch_after取消Operation Queue,并且延迟时间越来越长

时间:2016-05-05 19:10:34

标签: ios grand-central-dispatch nsoperationqueue

我有一个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运行。任何想法或建议

2 个答案:

答案 0 :(得分:1)

dispatch_after没有取消机制。能够取消是NSOperationQueue的优势之一。因此,不要直接在dispatch_after块中执行此工作,而是使用该块来安排NSOperation,然后也可以取消。{/ p>

答案 1 :(得分:0)

正如Curmudgeonlybumbly提到的,我使用了NSOperation。但我错过的是一旦操作开始取消没有做任何事情。我在操作中有一个迭代。所以我检查了self.cancelled并返回。有效。感谢