Created
October 12, 2012 20:06
-
-
Save codeswimmer/3881227 to your computer and use it in GitHub Desktop.
iOS:OSX: Timer Using 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
-(dispatch_source_t)createTimerWithQueue:(dispatch_queue_t)queue block:(dispatch_block_t)handler | |
{ | |
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); | |
if (timer) { | |
uint64_t seconds = 30ull; | |
uint64_t interval = seconds * NSEC_PER_SEC; | |
uint64_t leeway = 1ull * NSEC_PER_SEC; | |
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway); | |
dispatch_source_set_event_handler(timer, handler); | |
dispatch_resume(timer); | |
} | |
return timer; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment