Skip to content

Instantly share code, notes, and snippets.

@hfossli-agens
Created January 30, 2013 20:45
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save hfossli-agens/4676773 to your computer and use it in GitHub Desktop.
Save hfossli-agens/4676773 to your computer and use it in GitHub Desktop.
dispatch_after with repeat / loop
static void dispatch_repeated_internal(dispatch_time_t firstPopTime, double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop))
{
__block BOOL shouldStop = NO;
dispatch_time_t nextPopTime = dispatch_time(firstPopTime, (int64_t)(intervalInSeconds * NSEC_PER_SEC));
dispatch_after(nextPopTime, queue, ^{
work(&shouldStop);
if(!shouldStop)
{
dispatch_repeated_internal(nextPopTime, intervalInSeconds, queue, work);
}
});
}
void dispatch_repeated(double intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop))
{
dispatch_time_t firstPopTime = dispatch_time(DISPATCH_TIME_NOW, intervalInSeconds * NSEC_PER_SEC);
dispatch_repeated_internal(firstPopTime, intervalInSeconds, queue, work);
}
@kunlqt
Copy link

kunlqt commented Oct 29, 2014

How to stop repeat queue bro?

@ejsing
Copy link

ejsing commented Nov 6, 2014

I'd like to know as well.

Also, could you clarify how the function should be called? I'm using it in an iOS app.

@mluu510
Copy link

mluu510 commented Jul 27, 2016

set stop to YES

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment