Skip to content

Instantly share code, notes, and snippets.

@rais38
Created March 26, 2014 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rais38/9775642 to your computer and use it in GitHub Desktop.
Save rais38/9775642 to your computer and use it in GitHub Desktop.
Semaphor for wait a action.
//
// 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
//
// 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