Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. @interface JRMutableDictionary : NSMutableDictionary
  2. @end
  3.  
  4. @implementation JRMutableDictionary {
  5. dispatch_queue_t queue;
  6. NSMutableDictionary *storage;
  7. }
  8.  
  9. - (void)dealloc {
  10. if(queue) {
  11. dispatch_release(queue);
  12. queue = NULL;
  13. }
  14. }
  15.  
  16. - (id)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys {
  17. self = [super init];
  18. if(self) {
  19. queue = dispatch_queue_create("JRMutableDictionaryDispatchQueue", DISPATCH_QUEUE_CONCURRENT);
  20. storage = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys];
  21. }
  22. return self;
  23. }
  24.  
  25. #pragma mark - NSDictionary
  26.  
  27. - (NSUInteger)count {
  28. __block NSUInteger cnt = 0;
  29. dispatch_sync(queue, ^{
  30. cnt = [storage count];
  31. });
  32. return cnt;
  33. }
  34.  
  35. - (NSEnumerator *)keyEnumerator {
  36. __block NSEnumerator *enumerator = nil;
  37. dispatch_sync(queue, ^{
  38. enumerator = [storage keyEnumerator];
  39. });
  40. return enumerator;
  41. }
  42.  
  43. - (id)objectForKey:(id)aKey {
  44. __block id object = nil;
  45. dispatch_sync(queue, ^{
  46. object = [storage objectForKey:aKey];
  47. });
  48. return object;
  49. }
  50.  
  51. #pragma mark - NSMutableDictionary
  52.  
  53. - (void)setObject:(id)anObject forKey:(id)aKey {
  54. dispatch_barrier_async(queue, ^{
  55. [storage setObject:anObject forKey:aKey];
  56. });
  57. }
  58.  
  59. - (void)removeObjectForKey:(id)aKey {
  60. dispatch_barrier_async(queue, ^{
  61. [storage removeObjectForKey:aKey];
  62. });
  63. }
  64.  
  65. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement