且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

取消dispatch_after()方法?

更新时间:2023-02-09 18:05:43

没有办法阻止dispatch_block一旦发送到它的队列就执行,这意味着你的dispatch_after无法取消。唯一的选择是在块中添加要在运行时检查的条件以防止执行。
ie。

There is NO way to prevent a dispatch_block from executing once it has been dispatch to it's queue, meaning that your dispatch_after cannot be canceled. Only option is to add in your block a condition to be checked at runtime to prevent execution. ie.

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^ {
if(self.shouldExecuteDispatchBlock)
{ // do your stuff }  });