Created
March 26, 2014 02:05
-
-
Save rais38/9775642 to your computer and use it in GitHub Desktop.
Semaphor for wait a action.
This file contains hidden or 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
// | |
// BETTestSemaphor.h | |
// TwitterClient | |
// | |
// Created by Rafael Aguilar Martín on 11/11/12. | |
// Copyright (c) 2012 Rafael Aguilar Martín. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface BETTestSemaphor : NSObject | |
@property (strong, atomic) NSMutableDictionary* flags; | |
+ (BETTestSemaphor *)sharedInstance; | |
- (BOOL)isLifted:(NSString*)key; | |
- (void)lift:(NSString*)key; | |
- (void)waitForKey:(NSString*)key; | |
@end |
This file contains hidden or 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
// | |
// BETTestSemaphor.m | |
// TwitterClient | |
// | |
// Created by Rafael Aguilar Martín on 11/11/12. | |
// Copyright (c) 2012 Rafael Aguilar Martín. All rights reserved. | |
// | |
#import "BETTestSemaphor.h" | |
@implementation BETTestSemaphor | |
+ (BETTestSemaphor *)sharedInstance | |
{ | |
static BETTestSemaphor *sharedInstance = nil; | |
static dispatch_once_t once; | |
dispatch_once(&once, ^{ | |
sharedInstance = [BETTestSemaphor alloc]; | |
sharedInstance = [sharedInstance init]; | |
}); | |
return sharedInstance; | |
} | |
- (instancetype)init | |
{ | |
self = [super init]; | |
if (self != nil) { | |
self.flags = [NSMutableDictionary dictionaryWithCapacity:10]; | |
} | |
return self; | |
} | |
- (BOOL)isLifted:(NSString*)key | |
{ | |
return [self.flags objectForKey:key]!=nil; | |
} | |
- (void)lift:(NSString*)key | |
{ | |
[self.flags setObject:@"YES" forKey: key]; | |
} | |
- (void)waitForKey:(NSString*)key | |
{ | |
BOOL keepRunning = YES; | |
while (keepRunning && [[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:1.0]]) { | |
keepRunning = ![[BETTestSemaphor sharedInstance] isLifted: key]; | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment