Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
GCD timer
static dispatch_source_t CreateDispatchTimer(uint64_t interval, uint64_t leeway, dispatch_queue_t queue, dispatch_block_t block) {
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,
0, 0, queue);
if (timer) {
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
@interface MyClass {
dispatch_source_t _timer;
}
@implementation MyClass
- (void)invalidateTimer {
if (_timer) {
dispatch_source_cancel(_timer);
dispatch_release(_timer);
}
}
- (void)startTimer {
_timer = CreateDispatchTimer(1 * NSEC_PER_SEC, .01 * NSEC_PER_SEC, dispatch_get_main_queue(), ^{[self realTimeTick];});
}
- (void)realTimeTick {
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment