Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
Created October 12, 2012 20:06
Show Gist options
  • Save codeswimmer/3881227 to your computer and use it in GitHub Desktop.
Save codeswimmer/3881227 to your computer and use it in GitHub Desktop.
iOS:OSX: Timer Using GCD
-(dispatch_source_t)createTimerWithQueue:(dispatch_queue_t)queue block:(dispatch_block_t)handler
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (timer) {
uint64_t seconds = 30ull;
uint64_t interval = seconds * NSEC_PER_SEC;
uint64_t leeway = 1ull * NSEC_PER_SEC;
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
dispatch_source_set_event_handler(timer, handler);
dispatch_resume(timer);
}
return timer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment