Last active
August 29, 2015 14:25
-
-
Save maximbilan/5532309680284cabe4cf to your computer and use it in GitHub Desktop.
iOS Timer in not main thread
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 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; | |
} | |
dispatch_source_t timer = CreateDispatchTimer(1ull * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
// Repeating task | |
}); | |
dispatch_source_cancel(timer); | |
timer = nil; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment