-
-
Save karnlund/44b16d96effff998ff53 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 void dispatch_async_repeated_internal(dispatch_time_t firstPopTime, NSTimeInterval intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop)) | |
{ | |
dispatch_after(firstPopTime, queue, ^{ | |
BOOL shouldStop = NO; | |
work(&shouldStop); | |
if(!shouldStop) | |
{ | |
dispatch_time_t nextPopTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(intervalInSeconds * NSEC_PER_SEC)); | |
dispatch_async_repeated_internal(nextPopTime, intervalInSeconds, queue, work); | |
} | |
}); | |
} | |
void dispatch_async_repeated(NSTimeInterval intervalInSeconds, dispatch_queue_t queue, void(^work)(BOOL *stop)) | |
{ | |
dispatch_time_t firstPopTime = dispatch_time(DISPATCH_TIME_NOW, intervalInSeconds * NSEC_PER_SEC); | |
dispatch_async_repeated_internal(firstPopTime, intervalInSeconds, queue, work); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is no need for the __block variable.
And this modification has additional protection against work that takes longer than intervalInSeconds.