Last active
December 16, 2015 14:09
-
-
Save alyssais/5447015 to your computer and use it in GitHub Desktop.
This is a queue manager I quickly threw together last night. Basically just manages some dispatch_queue_t objects and organises them based on arbitrary integer keys you pass. Will automatically create queues when requested.
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 RAPQueueManager : NSObject | |
{ | |
NSMutableDictionary *queues; | |
} | |
@property (retain) NSString *label; | |
- (id)initWithLabel:(NSString *)label; | |
- (dispatch_queue_t)queueAtIndex:(NSInteger)index; | |
- (void)removeQueueAtIndex:(NSInteger)index; | |
@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 "RAPQueueManager.h" | |
@implementation RAPQueueManager | |
- (id)initWithLabel:(NSString *)label | |
{ | |
self = [self init]; | |
if (self) { | |
self.label = label; | |
} | |
return self; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
queues = [[NSMutableDictionary alloc] init]; | |
} | |
return self; | |
} | |
- (dispatch_queue_t)queueAtIndex:(NSInteger)index | |
{ | |
NSNumber *indexObject = @(index); | |
dispatch_queue_t queue = queues[indexObject]; | |
if (queue) return queue; | |
queue = dispatch_queue_create([self.label UTF8String], NULL); | |
queues[indexObject] = queue; | |
return queue; | |
} | |
- (void)removeQueueAtIndex:(NSInteger)index | |
{ | |
[queues removeObjectForKey:@(index)]; | |
} | |
- (void)dealloc | |
{ | |
for (dispatch_queue_t queue in queues) | |
{ | |
dispatch_release(queue); | |
[queues removeObjectsForKeys:[queues allKeysForObject:queue]]; | |
} | |
[queues release]; | |
queues = nil; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment