Skip to content

Instantly share code, notes, and snippets.

@lukewar
Created April 9, 2015 19:46
Show Gist options
  • Save lukewar/5a6e56847e737a12ac4a to your computer and use it in GitHub Desktop.
Save lukewar/5a6e56847e737a12ac4a to your computer and use it in GitHub Desktop.
Implementation of watchdog using GCD
CGFloat timeout = 2;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(_timer, dispatch_time(DISPATCH_TIME_NOW, timeout * NSEC_PER_SEC), DISPATCH_TIME_FOREVER, 0);
dispatch_source_set_event_handler(_timer, ^{
// custom code
dispatch_source_cancel(_timer);
});
dispatch_resume(_timer);
//
dispatch_source_cancel(_timer);
dispatch_release(_timer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment