@@ -23,6 +23,11 @@ @interface FUISortedArray ()
23
23
*/
24
24
@property (nonatomic , copy , nonnull ) NSComparisonResult (^sortDescriptor)(FIRDataSnapshot *, FIRDataSnapshot *);
25
25
26
+ /* *
27
+ * The backing collection that holds all of the array's data.
28
+ */
29
+ @property (strong , nonatomic ) NSMutableArray <FIRDataSnapshot *> *snapshots;
30
+
26
31
/* *
27
32
* A set containing the query observer handles that should be released when
28
33
* this array is freed.
@@ -32,6 +37,9 @@ @interface FUISortedArray ()
32
37
@end
33
38
34
39
@implementation FUISortedArray
40
+ // Cheating at subclassing, but this @dynamic avoids
41
+ // duplicating storage without exposing mutability publicly
42
+ @dynamic snapshots;
35
43
36
44
- (instancetype )initWithQuery : (FIRDatabaseQuery *)query
37
45
delegate : (id <FUICollectionDelegate>)delegate
@@ -57,7 +65,7 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
57
65
NSInteger index = [self indexForKey: snap.key];
58
66
if (index == NSNotFound ) { /* error */ return ; }
59
67
60
- [self removeSnapshotAtIndex :index ];
68
+ [self .snapshots removeObjectAtIndex :index ];
61
69
if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
62
70
[self .delegate array: self didRemoveObject: snap atIndex: index ];
63
71
}
@@ -71,7 +79,7 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
71
79
72
80
// Since changes can change ordering, model changes as a deletion and an insertion.
73
81
FIRDataSnapshot *removed = [self snapshotAtIndex: index ];
74
- [self removeSnapshotAtIndex :index ];
82
+ [self .snapshots removeObjectAtIndex :index ];
75
83
if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
76
84
[self .delegate array: self didRemoveObject: removed atIndex: index ];
77
85
}
@@ -92,29 +100,29 @@ - (NSArray *)items {
92
100
93
101
- (NSInteger )insertSnapshot : (FIRDataSnapshot *)snapshot {
94
102
if (self.count == 0 ) {
95
- [self addSnapshot : snapshot];
103
+ [self .snapshots addObject : snapshot];
96
104
return 0 ;
97
105
}
98
106
if (self.count == 1 ) {
99
107
NSComparisonResult result = self.sortDescriptor (snapshot, [self snapshotAtIndex: 0 ]);
100
108
switch (result) {
101
109
case NSOrderedDescending:
102
- [self addSnapshot : snapshot];
110
+ [self .snapshots addObject : snapshot];
103
111
return 1 ;
104
112
default :
105
- [self insertSnapshot : snapshot atIndex: 0 ];
113
+ [self .snapshots insertObject : snapshot atIndex: 0 ];
106
114
return 0 ;
107
115
}
108
116
}
109
117
110
118
NSInteger index = self.count / 2 ;
111
119
while (index >= 0 && index <= self.count ) {
112
120
if (index == 0 ) {
113
- [self insertSnapshot : snapshot atIndex: index ];
121
+ [self .snapshots insertObject : snapshot atIndex: index ];
114
122
return 0 ;
115
123
}
116
124
if (index == self.count ) {
117
- [self addSnapshot : snapshot];
125
+ [self .snapshots addObject : snapshot];
118
126
return index ;
119
127
}
120
128
@@ -136,7 +144,7 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
136
144
NSAssert (NO , @" FUISortedArray %@ 's sort descriptor returned inconsistent results!" , self);
137
145
} else {
138
146
// good
139
- [self insertSnapshot : snapshot atIndex: index ];
147
+ [self .snapshots insertObject : snapshot atIndex: index ];
140
148
return index ;
141
149
}
142
150
}
0 commit comments