@@ -23,6 +23,11 @@ @interface FUISortedArray ()
2323 */
2424@property (nonatomic , copy , nonnull ) NSComparisonResult (^sortDescriptor)(FIRDataSnapshot *, FIRDataSnapshot *);
2525
26+ /* *
27+ * The backing collection that holds all of the array's data.
28+ */
29+ @property (strong , nonatomic ) NSMutableArray <FIRDataSnapshot *> *snapshots;
30+
2631/* *
2732 * A set containing the query observer handles that should be released when
2833 * this array is freed.
@@ -32,6 +37,9 @@ @interface FUISortedArray ()
3237@end
3338
3439@implementation FUISortedArray
40+ // Cheating at subclassing, but this @dynamic avoids
41+ // duplicating storage without exposing mutability publicly
42+ @dynamic snapshots;
3543
3644- (instancetype )initWithQuery : (FIRDatabaseQuery *)query
3745 delegate : (id <FUICollectionDelegate>)delegate
@@ -57,7 +65,7 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
5765 NSInteger index = [self indexForKey: snap.key];
5866 if (index == NSNotFound ) { /* error */ return ; }
5967
60- [self removeSnapshotAtIndex : index];
68+ [self .snapshots removeObjectAtIndex : index];
6169 if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
6270 [self .delegate array: self didRemoveObject: snap atIndex: index];
6371 }
@@ -71,7 +79,7 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
7179
7280 // Since changes can change ordering, model changes as a deletion and an insertion.
7381 FIRDataSnapshot *removed = [self snapshotAtIndex: index];
74- [self removeSnapshotAtIndex : index];
82+ [self .snapshots removeObjectAtIndex : index];
7583 if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
7684 [self .delegate array: self didRemoveObject: removed atIndex: index];
7785 }
@@ -92,29 +100,29 @@ - (NSArray *)items {
92100
93101- (NSInteger )insertSnapshot : (FIRDataSnapshot *)snapshot {
94102 if (self.count == 0 ) {
95- [self addSnapshot : snapshot];
103+ [self .snapshots addObject : snapshot];
96104 return 0 ;
97105 }
98106 if (self.count == 1 ) {
99107 NSComparisonResult result = self.sortDescriptor (snapshot, [self snapshotAtIndex: 0 ]);
100108 switch (result) {
101109 case NSOrderedDescending:
102- [self addSnapshot : snapshot];
110+ [self .snapshots addObject : snapshot];
103111 return 1 ;
104112 default :
105- [self insertSnapshot : snapshot atIndex: 0 ];
113+ [self .snapshots insertObject : snapshot atIndex: 0 ];
106114 return 0 ;
107115 }
108116 }
109117
110118 NSInteger index = self.count / 2 ;
111119 while (index >= 0 && index <= self.count ) {
112120 if (index == 0 ) {
113- [self insertSnapshot : snapshot atIndex: index];
121+ [self .snapshots insertObject : snapshot atIndex: index];
114122 return 0 ;
115123 }
116124 if (index == self.count ) {
117- [self addSnapshot : snapshot];
125+ [self .snapshots addObject : snapshot];
118126 return index;
119127 }
120128
@@ -136,7 +144,7 @@ - (NSInteger)insertSnapshot:(FIRDataSnapshot *)snapshot {
136144 NSAssert (NO , @" FUISortedArray %@ 's sort descriptor returned inconsistent results!" , self);
137145 } else {
138146 // good
139- [self insertSnapshot : snapshot atIndex: index];
147+ [self .snapshots insertObject : snapshot atIndex: index];
140148 return index;
141149 }
142150 }
0 commit comments