Last active
December 22, 2015 23:19
-
-
Save hunje/6545742 to your computer and use it in GitHub Desktop.
The helper class for asynchronous wait with timeout and simple signal.
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
#import <Foundation/Foundation.h> | |
@interface HTWait : NSObject | |
{ | |
BOOL signaled; | |
} | |
- (void)prepare; | |
- (void)signal; | |
- (void)waitForTimeout:(NSTimeInterval)timeOut; | |
@end |
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
#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