@@ -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
@@ -63,16 +63,7 @@ - (void)observeQuery {
63
63
FIRDatabaseHandle handle;
64
64
handle = [self .query observeEventType: FIRDataEventTypeChildAdded
65
65
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
66
- NSUInteger index = 0 ;
67
- if (previousChildKey != nil ) {
68
- index = [self indexForKey: previousChildKey] + 1 ;
69
- }
70
-
71
- [self .snapshots insertObject: snapshot atIndex: index ];
72
-
73
- if ([self .delegate respondsToSelector: @selector (array:didAddObject:atIndex: )]) {
74
- [self .delegate array: self didAddObject: snapshot atIndex: index ];
75
- }
66
+ [self insertSnapshot: snapshot withPreviousChildKey: previousChildKey];
76
67
}
77
68
withCancelBlock: ^(NSError *error) {
78
69
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -83,13 +74,7 @@ - (void)observeQuery {
83
74
84
75
handle = [self .query observeEventType: FIRDataEventTypeChildChanged
85
76
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
86
- NSUInteger index = [self indexForKey: snapshot.key];
87
-
88
- [self .snapshots replaceObjectAtIndex: index withObject: snapshot];
89
-
90
- if ([self .delegate respondsToSelector: @selector (array:didChangeObject:atIndex: )]) {
91
- [self .delegate array: self didChangeObject: snapshot atIndex: index ];
92
- }
77
+ [self changeSnapshot: snapshot withPreviousChildKey: previousChildKey];
93
78
}
94
79
withCancelBlock: ^(NSError *error) {
95
80
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -100,13 +85,7 @@ - (void)observeQuery {
100
85
101
86
handle = [self .query observeEventType: FIRDataEventTypeChildRemoved
102
87
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousSiblingKey) {
103
- NSUInteger index = [self indexForKey: snapshot.key];
104
-
105
- [self .snapshots removeObjectAtIndex: index ];
106
-
107
- if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
108
- [self .delegate array: self didRemoveObject: snapshot atIndex: index ];
109
- }
88
+ [self removeSnapshot: snapshot withPreviousChildKey: previousSiblingKey];
110
89
}
111
90
withCancelBlock: ^(NSError *error) {
112
91
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -117,21 +96,7 @@ - (void)observeQuery {
117
96
118
97
handle = [self .query observeEventType: FIRDataEventTypeChildMoved
119
98
andPreviousSiblingKeyWithBlock: ^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
120
- NSUInteger fromIndex = [self indexForKey: snapshot.key];
121
- [self .snapshots removeObjectAtIndex: fromIndex];
122
-
123
- NSUInteger toIndex = 0 ;
124
- if (previousChildKey != nil ) {
125
- NSUInteger prevIndex = [self indexForKey: previousChildKey];
126
- if (prevIndex != NSNotFound ) {
127
- toIndex = prevIndex + 1 ;
128
- }
129
- }
130
- [self .snapshots insertObject: snapshot atIndex: toIndex];
131
-
132
- if ([self .delegate respondsToSelector: @selector (array:didMoveObject:fromIndex:toIndex: )]) {
133
- [self .delegate array: self didMoveObject: snapshot fromIndex: fromIndex toIndex: toIndex];
134
- }
99
+ [self moveSnapshot: snapshot withPreviousChildKey: previousChildKey];
135
100
}
136
101
withCancelBlock: ^(NSError *error) {
137
102
if ([self .delegate respondsToSelector: @selector (array:queryCancelledWithError: )]) {
@@ -158,6 +123,69 @@ - (NSUInteger)indexForKey:(NSString *)key {
158
123
return NSNotFound ;
159
124
}
160
125
126
+ - (void )insertSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
127
+ NSUInteger index = 0 ;
128
+ if (previous != nil ) {
129
+ index = [self indexForKey: previous] + 1 ;
130
+ }
131
+
132
+ [self .snapshots insertObject: snap atIndex: index ];
133
+
134
+ if ([self .delegate respondsToSelector: @selector (array:didAddObject:atIndex: )]) {
135
+ [self .delegate array: self didAddObject: snap atIndex: index ];
136
+ }
137
+ }
138
+
139
+ - (void )removeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
140
+ NSUInteger index = [self indexForKey: snap.key];
141
+
142
+ [self .snapshots removeObjectAtIndex: index ];
143
+
144
+ if ([self .delegate respondsToSelector: @selector (array:didRemoveObject:atIndex: )]) {
145
+ [self .delegate array: self didRemoveObject: snap atIndex: index ];
146
+ }
147
+ }
148
+
149
+ - (void )changeSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
150
+ NSUInteger index = [self indexForKey: snap.key];
151
+
152
+ [self .snapshots replaceObjectAtIndex: index withObject: snap];
153
+
154
+ if ([self .delegate respondsToSelector: @selector (array:didChangeObject:atIndex: )]) {
155
+ [self .delegate array: self didChangeObject: snap atIndex: index ];
156
+ }
157
+ }
158
+
159
+ - (void )moveSnapshot : (FIRDataSnapshot *)snap withPreviousChildKey : (NSString *)previous {
160
+ NSUInteger fromIndex = [self indexForKey: snap.key];
161
+ [self .snapshots removeObjectAtIndex: fromIndex];
162
+
163
+ NSUInteger toIndex = 0 ;
164
+ if (previous != nil ) {
165
+ NSUInteger prevIndex = [self indexForKey: previous];
166
+ if (prevIndex != NSNotFound ) {
167
+ toIndex = prevIndex + 1 ;
168
+ }
169
+ }
170
+ [self .snapshots insertObject: snap atIndex: toIndex];
171
+
172
+ if ([self .delegate respondsToSelector: @selector (array:didMoveObject:fromIndex:toIndex: )]) {
173
+ [self .delegate array: self didMoveObject: snap fromIndex: fromIndex toIndex: toIndex];
174
+ }
175
+ }
176
+
177
+ - (void )removeSnapshotAtIndex : (NSUInteger )index {
178
+ [self .snapshots removeObjectAtIndex: index ];
179
+ }
180
+
181
+ - (void )insertSnapshot : (FIRDataSnapshot *)snap atIndex : (NSUInteger )index {
182
+ [self .snapshots insertObject: snap atIndex: index ];
183
+ }
184
+
185
+ - (void )addSnapshot : (FIRDataSnapshot *)snap {
186
+ [self .snapshots addObject: snap];
187
+ }
188
+
161
189
#pragma mark - Public API methods
162
190
163
191
- (NSArray *)items {
0 commit comments