取消dispatch_after()方法? [英] cancel dispatch_after() method?
问题描述
有没有办法取消将来某段时间安排的 dispatch_after(),到目前为止还没有解雇?
我正在尝试为服务器更新调度程序,这个方法就像我想要的那样,但是,我喜欢在某些时候取消并重新安排它。
是否有可能或我必须回退并使用NSTimer?
Is there a way to cancel dispatch_after() scheduled for some time in future, and haven't fired so far? I'm trying to make something like a scheduler for updates from server, and this method is just like I want, but, I'd love to cancel and re-schedule it at some point. Is it possible at all or I have to fallback and use NSTimer?
推荐答案
没有办法阻止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 } });
这篇关于取消dispatch_after()方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!