Skip to content

Instantly share code, notes, and snippets.

@XVXVXXX
Forked from jagreenwood/GCD Timer.m
Created June 13, 2017 08:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XVXVXXX/4d3b856972b4dc8e4107ae64edb2effe to your computer and use it in GitHub Desktop.
Save XVXVXXX/4d3b856972b4dc8e4107ae64edb2effe to your computer and use it in GitHub Desktop.
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