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 NSRunLoop (Condition) | |
-(BOOL)runInDefaultModeWhileCondition:(BOOL *)condition inIntervals:(NSTimeInterval) quantum; | |
-(BOOL)runWhileCondition:(BOOL *)condition inMode:(NSString *)mode inIntervals:(NSTimeInterval) quantum; | |
@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 "NSRunLoop+Condition.h" | |
@implementation NSRunLoop (Condition) | |
-(BOOL)runInDefaultModeWhileCondition:(BOOL *)condition inIntervals:(NSTimeInterval) quantum { | |
return [self runWhileCondition:condition inMode:NSDefaultRunLoopMode inIntervals:quantum]; | |
} | |
-(BOOL)runWhileCondition:(BOOL *)condition inMode:(NSString *)mode inIntervals:(NSTimeInterval) quantum { | |
BOOL didRun = NO; | |
BOOL shouldRun = YES; | |
NSPort *dummyPort = [NSMachPort port]; | |
[self addPort:dummyPort forMode:NSDefaultRunLoopMode]; | |
while (shouldRun) { | |
@autoreleasepool { | |
didRun = [self runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:quantum]]; | |
shouldRun = (didRun ? *condition : NO); | |
} | |
} | |
[self removePort:dummyPort forMode:NSDefaultRunLoopMode]; | |
return didRun; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is BSD Licensed