Last active
August 29, 2015 14:19
-
-
Save sprohaszka/86663b5e61cf7994393f to your computer and use it in GitHub Desktop.
Create a timer (redundant task) with GCD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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