@@ -25,13 +25,13 @@ @interface FUIArray ()
25
25
/* *
26
26
* The backing collection that holds all of the array's data.
27
27
*/
28
- @property (strong , nonatomic ) NSMutableArray <FIRDataSnapshot *> *snapshots;
28
+ @property (strong , nonatomic ) NSMutableArray <FIRDataSnapshot *> *snapshots;
29
29
30
30
/* *
31
31
* A set containing the query observer handles that should be released when
32
32
* this array is freed.
33
33
*/
34
- @property (strong , nonatomic ) NSMutableSet <NSNumber *> *handles;
34
+ @property (strong , nonatomic ) NSMutableSet <NSNumber *> *handles;
35
35
36
36
@end
37
37
@@ -72,16 +72,7 @@ - (void)observeQuery {
72
72
FIRDatabaseHandle handle;
73
73
handle = [self .query observeEventType: FIRDataEventTypeChildAdded
74
74
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
75
- NSUInteger index = 0 ;
76
- if (previousChildKey != nil ) {
77
- index = [self indexForKey: previousChildKey] + 1 ;
78
- }
79
-
80
- [self .snapshots insertObject: snapshot atIndex: index ];
81
-
82
- if ([self .delegate respondsToSelector: @selector (array:didAddObject:atIndex: )]) {
83
- [self .delegate array: self didAddObject: snapshot atIndex: index ];
84
- }
75
+ [self insertSnapshot: snapshot withPreviousChildKey: previousChildKey];
85
76
}
86
77
withCancelBlock: ^(NSError *error) {
87
78
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -92,13 +83,7 @@ - (void)observeQuery {
92
83
93
84
handle = [self .query observeEventType: FIRDataEventTypeChildChanged
94
85
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
95
- NSUInteger index = [self indexForKey: snapshot.key];
96
-
97
- [self .snapshots replaceObjectAtIndex: index withObject: snapshot];
98
-
99
- if ([self .delegate respondsToSelector: @selector (array:didChangeObject:atIndex: )]) {
100
- [self .delegate array: self didChangeObject: snapshot atIndex: index ];
101
- }
86
+ [self changeSnapshot: snapshot withPreviousChildKey: previousChildKey];
102
87
}
103
88
withCancelBlock: ^(NSError *error) {
104
89
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -109,13 +94,7 @@ - (void)observeQuery {
109
94
110
95
handle = [self .query observeEventType: FIRDataEventTypeChildRemoved
111
96
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousSiblingKey) {
112
- NSUInteger index = [self indexForKey: snapshot.key];
113
-
114
- [self .snapshots removeObjectAtIndex: index ];
115
-
116
- if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
117
- [self .delegate array: self didRemoveObject: snapshot atIndex: index ];
118
- }
97
+ [self removeSnapshot: snapshot withPreviousChildKey: previousSiblingKey];
119
98
}
120
99
withCancelBlock: ^(NSError *error) {
121
100
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -126,22 +105,7 @@ - (void)observeQuery {
126
105
127
106
handle = [self .query observeEventType: FIRDataEventTypeChildMoved
128
107
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
129
- NSUInteger fromIndex = [self indexForKey: snapshot.key];
130
- [self .snapshots removeObjectAtIndex: fromIndex];
131
-
132
- NSUInteger toIndex = 0 ;
133
- if (previousChildKey != nil ) {
134
- NSUInteger prevIndex = [self indexForKey: previousChildKey];
135
- if (prevIndex != NSNotFound ) {
136
- toIndex = prevIndex + 1 ;
137
- }
138
- }
139
-
140
- [self .snapshots insertObject: snapshot atIndex: toIndex];
141
-
142
- if ([self .delegate respondsToSelector: @selector (array:didMoveObject:fromIndex:toIndex: )]) {
143
- [self .delegate array: self didMoveObject: snapshot fromIndex: fromIndex toIndex: toIndex];
144
- }
108
+ [self moveSnapshot: snapshot withPreviousChildKey: previousChildKey];
145
109
}
146
110
withCancelBlock: ^(NSError *error) {
147
111
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -168,6 +132,69 @@ - (NSUInteger)indexForKey:(NSString *)key {
168
132
return NSNotFound ;
169
133
}
170
134
135
+ - (void )insertSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
136
+ NSUInteger index = 0 ;
137
+ if (previous != nil ) {
138
+ index = [self indexForKey: previous] + 1 ;
139
+ }
140
+
141
+ [self .snapshots insertObject: snap atIndex: index ];
142
+
143
+ if ([self .delegate respondsToSelector: @selector (array:didAddObject:atIndex: )]) {
144
+ [self .delegate array: self didAddObject: snap atIndex: index ];
145
+ }
146
+ }
147
+
148
+ - (void )removeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
149
+ NSUInteger index = [self indexForKey: snap.key];
150
+
151
+ [self .snapshots removeObjectAtIndex: index ];
152
+
153
+ if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
154
+ [self .delegate array: self didRemoveObject: snap atIndex: index ];
155
+ }
156
+ }
157
+
158
+ - (void )changeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
159
+ NSUInteger index = [self indexForKey: snap.key];
160
+
161
+ [self .snapshots replaceObjectAtIndex: index withObject: snap];
162
+
163
+ if ([self .delegate respondsToSelector: @selector (array:didChangeObject:atIndex: )]) {
164
+ [self .delegate array: self didChangeObject: snap atIndex: index ];
165
+ }
166
+ }
167
+
168
+ - (void )moveSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
169
+ NSUInteger fromIndex = [self indexForKey: snap.key];
170
+ [self .snapshots removeObjectAtIndex: fromIndex];
171
+
172
+ NSUInteger toIndex = 0 ;
173
+ if (previous != nil ) {
174
+ NSUInteger prevIndex = [self indexForKey: previous];
175
+ if (prevIndex != NSNotFound ) {
176
+ toIndex = prevIndex + 1 ;
177
+ }
178
+ }
179
+ [self .snapshots insertObject: snap atIndex: toIndex];
180
+
181
+ if ([self .delegate respondsToSelector: @selector (array:didMoveObject:fromIndex:toIndex: )]) {
182
+ [self .delegate array: self didMoveObject: snap fromIndex: fromIndex toIndex: toIndex];
183
+ }
184
+ }
185
+
186
+ - (void )removeSnapshotAtIndex : (NSUInteger )index {
187
+ [self .snapshots removeObjectAtIndex: index ];
188
+ }
189
+
190
+ - (void )insertSnapshot : (FIRDataSnapshot *)snap atIndex : (NSUInteger )index {
191
+ [self .snapshots insertObject: snap atIndex: index ];
192
+ }
193
+
194
+ - (void )addSnapshot : (FIRDataSnapshot *)snap {
195
+ [self .snapshots addObject: snap];
196
+ }
197
+
171
198
#pragma mark - Public API methods
172
199
173
200
- (NSArray *)items {
0 commit comments