-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIRObjectQueue.m
More file actions
110 lines (67 loc) · 2.06 KB
/
IRObjectQueue.m
File metadata and controls
110 lines (67 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// IRObjectQueue.m
// IRObjectQueue
//
// Created by Evadne Wu on 6/1/12.
// Copyright (c) 2012 Iridia Productions. All rights reserved.
//
#import "IRObjectQueue.h"
#import <TargetConditionals.h>
#if TARGET_OS_IPHONE
#import <UIKit/UIKit.h>
#endif
@interface IRObjectQueue ()
@property (nonatomic, readwrite, strong) NSMutableDictionary *identifierToObjects;
- (NSMutableSet *) mutableSetForIdentifier:(NSString *)identifier;
@end
@implementation IRObjectQueue
@synthesize identifierToObjects = _identifierToObjects;
@synthesize purgesAutomatically = _purgesAutomatically;
- (id) init {
self = [super init];
if (!self)
return nil;
_purgesAutomatically = YES;
#if TARGET_OS_IPHONE
__weak IRObjectQueue *wSelf = self;
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
[wSelf handleMemoryWarning:note];
}];
#endif
return self;
}
- (NSMutableDictionary *) identifierToObjects {
if (!_identifierToObjects)
_identifierToObjects = [NSMutableDictionary dictionary];
return _identifierToObjects;
}
- (void) addObject:(id<IRQueueableObject>)object {
NSString *reuseID = [object reuseIdentifier];
NSParameterAssert(object);
NSParameterAssert(reuseID);
[[self mutableSetForIdentifier:reuseID] addObject:object];
}
- (id) dequeueObjectWithIdentifier:(NSString *)reuseID {
NSMutableSet *ms = [self mutableSetForIdentifier:reuseID];
id<IRQueueableObject> answer = [ms anyObject];
if (answer)
[ms removeObject:answer];
return answer;
}
- (void) removeAllObjects {
self.identifierToObjects = nil;
}
- (NSMutableSet *) mutableSetForIdentifier:(NSString *)identifier {
NSMutableDictionary *idsToObjs = self.identifierToObjects;
NSMutableSet *answer = [idsToObjs objectForKey:identifier];
if (!answer) {
answer = [NSMutableSet set];
[idsToObjs setObject:answer forKey:identifier];
}
return answer;
}
- (void) handleMemoryWarning:(NSNotification *)note {
if (self.purgesAutomatically)
[self removeAllObjects];
}
@end