Skip to content

Instantly share code, notes, and snippets.

@karpelcevs
Created August 25, 2014 15:47
Show Gist options
  • Save karpelcevs/7fa7f9e725a4cd3665d9 to your computer and use it in GitHub Desktop.
Save karpelcevs/7fa7f9e725a4cd3665d9 to your computer and use it in GitHub Desktop.
@implementation XCTest (Async)
+ (NSTimeInterval)waitTillReady:(BOOL (^)())isReady orTimeout:(NSTimeInterval)seconds
{
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:seconds];
while (!isReady() && [loopUntil timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil];
}
NSTimeInterval finish = [NSDate timeIntervalSinceReferenceDate];
return (finish - start);
}
+ (void)waitForTimeout:(NSTimeInterval)seconds
{
NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:seconds];
while ([loopUntil timeIntervalSinceNow] > 0) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment