Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cancel dispatch_after() method? [duplicate]

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?

like image 586
Oleg Shanyuk Avatar asked Jan 22 '15 11:01

Oleg Shanyuk


1 Answers

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 }  }); 
like image 182
Florian Burel Avatar answered Sep 21 '22 16:09

Florian Burel