Skip to content

Instantly share code, notes, and snippets.

@sprohaszka
Last active August 29, 2015 14:19
Show Gist options
  • Save sprohaszka/86663b5e61cf7994393f to your computer and use it in GitHub Desktop.
Save sprohaszka/86663b5e61cf7994393f to your computer and use it in GitHub Desktop.
Create a timer (redundant task) with GCD
static const NUInteger kFrequency = 15; //seconds
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (self.timer) {
dispatch_source_set_timer(self.timer,
dispatch_time(DISPATCH_TIME_NOW, LMConversationHelperPollingFrequency * NSEC_PER_SEC),
kFrequency * NSEC_PER_SEC,
(1ull * NSEC_PER_SEC) / 10);
__weak id weakSelf = self;
dispatch_source_set_event_handler(self.timer, ^{
// Secret task every 15 seconds
});
dispatch_resume(self.timer);
}
- (void)dealloc {
dispatch_source_cancel(_timer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment