Skip to content

Instantly share code, notes, and snippets.

@cruffenach
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cruffenach/8631aa1040fd40c31c4a to your computer and use it in GitHub Desktop.
Save cruffenach/8631aa1040fd40c31c4a to your computer and use it in GitHub Desktop.
void CRWaitMinimumDurationAndExecute(NSTimeInterval start, NSTimeInterval minimumDuration, void(^block)(void)) {
double diff = [NSDate date].timeIntervalSince1970-start;
double delayInSeconds = MAX(0.0, minimumDuration-diff);
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if (block) block();
});
}
//Usage
NSTimeInterval start = [NSDate date].timeIntervalSince1970;
NSTimeInterval minimumDuration = 2.0
[[SomeAPIClient sharedInstance] getWithCompletion:(void (^)(void))completion {
CRWaitMinimumDurationAndExecute(start, minimumDuration, ^{
//Stuff that will happen either 2 seconds later or whenever completion ends up getting called if that is > 2 seconds
});
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment