-
-
Save XVXVXXX/4d3b856972b4dc8e4107ae64edb2effe to your computer and use it in GitHub Desktop.
GCD timer
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 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