Last active
August 29, 2015 14:06
-
-
Save cruffenach/8631aa1040fd40c31c4a to your computer and use it in GitHub Desktop.
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
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