Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created October 16, 2012 22:48
Show Gist options
  • Save codeswimmer/3902570 to your computer and use it in GitHub Desktop.
Save codeswimmer/3902570 to your computer and use it in GitHub Desktop.
iOS: GCD Periodic Timer
dispatch_queue_t gcdTimerQueue;
dispatch_source_t gcdTimer;
gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, gcdTimerQueue);
if (gcdTimer) {
uint64_t seconds = 30ull;
uint64_t interval = seconds * NSEC_PER_SEC;
uint64_t leeway = 1ull * NSEC_PER_SEC;
__block typeof(self) _self = self;
dispatch_source_set_timer(gcdTimer, dispatch_walltime(NULL, 0), interval, leeway);
dispatch_source_set_event_handler(gcdTimer, ^{
// stuff to run on a periodic basis
});
dispatch_resume(gcdTimer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment