Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @interface JRMutableDictionary : NSMutableDictionary
- @end
- @implementation JRMutableDictionary {
- dispatch_queue_t queue;
- NSMutableDictionary *storage;
- }
- - (void)dealloc {
- if(queue) {
- dispatch_release(queue);
- queue = NULL;
- }
- }
- - (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
- self = [super init];
- if(self) {
- queue = dispatch_queue_create("JRMutableDictionaryDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
- storage = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
- }
- return self;
- }
- #pragma mark - NSDictionary
- - (NSUInteger)count {
- __block NSUInteger cnt = 0;
- dispatch_sync(queue, ^{
- cnt = [storage count];
- });
- return cnt;
- }
- - (NSEnumerator *)keyEnumerator {
- __block NSEnumerator *enumerator = nil;
- dispatch_sync(queue, ^{
- enumerator = [storage keyEnumerator];
- });
- return enumerator;
- }
- - (id)objectForKey:(id)aKey {
- __block id object = nil;
- dispatch_sync(queue, ^{
- object = [storage objectForKey:aKey];
- });
- return object;
- }
- #pragma mark - NSMutableDictionary
- - (void)setObject:(id)anObject forKey:(id)aKey {
- dispatch_barrier_async(queue, ^{
- [storage setObject:anObject forKey:aKey];
- });
- }
- - (void)removeObjectForKey:(id)aKey {
- dispatch_barrier_async(queue, ^{
- [storage removeObjectForKey:aKey];
- });
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement