Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Created January 30, 2015 20:27
Show Gist options
  • Save Air-Craft/c1a77ec3c314090175d9 to your computer and use it in GitHub Desktop.
Save Air-Craft/c1a77ec3c314090175d9 to your computer and use it in GitHub Desktop.
One-off timer with GCD on high priority thread #concurrency #GCD #objective-c #advanced
dispatch_source_t _timer;
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL, <#timeout#> * NSEC_PER_SEC),
1 * NSEC_PER_SEC, // this doenst really matter
<# 0.01 (tolerance) #> * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
dispatch_source_cancel(_timer); // cancel the repeat
// YOUR STUFF
});
dispatch_resume(_timer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment