ios - 如何使用for循环和dispatch_after取消操作队列并增加延迟

标签 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 运行。任何想法或建议

最佳答案

dispatch_after 没有取消机制。能够取消是NSOperationQueue 的优点之一。因此,不要直接在您的 dispatch_after block 中完成工作,而是使用该 block 来安排 NSOperation,然后也可以将其取消。

关于ios - 如何使用for循环和dispatch_after取消操作队列并增加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37058466/

相关文章:

ios - 你如何在 Swift 中匹配两个相似的电话号码,但一个包含国家代码而另一个包含或不包含国家代码

swift - DispatchGroup 未在 Swift 中通知 'Main"

swift - 为什么我们从线程中移出闭包在 Grand Central Dispatch 中被调用?

ios - NSOperationQueue 取消特定操作

iphone - 在后台线程上构建 UIViews

ios - 从 firebase + iOS 获取数据时加载数据缓慢

ios - 在 iOS : NSBundle is missing 中获取 Assets 文件的路径

ios - FaSTLane Match - 通过一次运行获取所有配置文件

ios - dispatch_group_notify 已被实例方法 DispatchGroup.notify(qos :flags:queue:execute:)

swift - 如何防止同时进行两次触摸