Skip to content

Instantly share code, notes, and snippets.

@kylehowells
Created August 4, 2014 19:34
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylehowells/f333625e96ff9c7ecf9b to your computer and use it in GitHub Desktop.
Save kylehowells/f333625e96ff9c7ecf9b to your computer and use it in GitHub Desktop.
Event queue I wrote to make a block run after all conditions have been met. For example, removing a complex loading animation after the animation has finished and the data has loaded.
//
// KHEventQueue.h
//
// Created by Kyle Howells on 04/08/2014.
// Copyright (c) 2014 Kyle Howells. All rights reserved.
//
#import <Foundation/Foundation.h>
typedef void (^KHEventBlock)(void);
@interface KHEventQueue : NSObject
+(instancetype)sharedInstance;
-(void)eventHappened:(NSString*)event;
// Block = code to run after all events have fired.
// requirements = array of NSString's.
-(void)addBlock:(KHEventBlock)block withRequirements:(NSArray*)requirements;
@end
//
// KHEventQueue.m
//
// Created by Kyle Howells on 04/08/2014.
// Copyright (c) 2014 Kyle Howells. All rights reserved.
//
#import "KHEventQueue.h"
NSString * const KH_EVENT_BLOCK_KEY = @"BLOCK_KEY";
NSString * const KH_EVENT_REQUIREMENTS_KEY = @"REQUIREMENTS_KEY";
#define BLOCK_KEY KH_EVENT_BLOCK_KEY
#define REQUIREMENTS_KEY KH_EVENT_REQUIREMENTS_KEY
@implementation KHEventQueue{
NSOperationQueue *accessQueue;
NSMutableArray *dictionarysWaiting;
}
+(instancetype)sharedInstance {
static dispatch_once_t pred;
static id shared = nil;
dispatch_once(&pred, ^{
shared = [[self alloc] init];
});
return shared;
}
-(instancetype)init{
if (self = [super init]) {
accessQueue = [[NSOperationQueue alloc] init];
accessQueue.maxConcurrentOperationCount = 1;
accessQueue.name = @"Custom Event Queue";
[accessQueue setSuspended:NO];
dictionarysWaiting = [[NSMutableArray alloc] init];
}
return self;
}
-(void)addBlock:(KHEventBlock)block withRequirements:(NSArray*)requirements{
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
NSMutableArray *mutableCopy = [requirements mutableCopy];
[dictionary setObject:mutableCopy forKey:REQUIREMENTS_KEY];
[mutableCopy release];
KHEventBlock blockCopy = [block copy];
[dictionary setObject:blockCopy forKey:BLOCK_KEY];
[blockCopy release];
[accessQueue addOperationWithBlock:^{
[dictionarysWaiting addObject:dictionary];
}];
}
-(void)eventHappened:(NSString*)event{
[accessQueue addOperationWithBlock:^{
for (NSInteger index = dictionarysWaiting.count - 1; index >= 0 ; index--)
{
NSDictionary *dicationary = dictionarysWaiting[index];
NSMutableArray *array = dicationary[REQUIREMENTS_KEY];
for (NSInteger i = array.count - 1; i >= 0 ; i--) {
NSString *string = array[i];
if ([string isEqualToString:event]) {
[array removeObjectAtIndex:i];
}
}
if (array.count == 0) {
KHEventBlock block = dicationary[BLOCK_KEY];
if (block != nil) {
block();
}
[dictionarysWaiting removeObjectAtIndex:index];
}
}
}];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment