Skip to content

Instantly share code, notes, and snippets.

@hunje
Last active December 22, 2015 23:19
Show Gist options
  • Save hunje/6545742 to your computer and use it in GitHub Desktop.
Save hunje/6545742 to your computer and use it in GitHub Desktop.
The helper class for asynchronous wait with timeout and simple signal.
#import <Foundation/Foundation.h>
@interface HTWait : NSObject
{
BOOL signaled;
}
- (void)prepare;
- (void)signal;
- (void)waitForTimeout:(NSTimeInterval)timeOut;
@end
#import "HTWait.h"
@implementation HTWait
- (void)prepare
{
signaled = NO;
}
- (void)signal
{
@synchronized(self) {
signaled = YES;
}
}
- (void)waitForTimeout:(NSTimeInterval)timeOut
{
NSDate* untilDate = [NSDate dateWithTimeIntervalSinceNow:timeOut];
NSTimeInterval timeInterval = 0.05;
while (signaled == NO)
{
if (![[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:timeInterval]])
{
[NSThread sleepForTimeInterval:timeInterval];
}
if ([untilDate compare:[NSDate date]] == NSOrderedAscending)
break;
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment