Skip to content

Commit 6e01687

Browse files
committed
optimize FUIArray’s indexForKey method by using an array of keys
1 parent 24bbf32 commit 6e01687

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Database/FirebaseDatabaseUI/FUIArray.m

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ @interface FUIArray ()
2727
*/
2828
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;
2929

30+
/**
31+
* The backing collection that holds all of the array's key.
32+
*/
33+
@property (strong, nonatomic) NSMutableArray<NSString *> *keys;
34+
3035
/**
3136
* A set containing the query observer handles that should be released when
3237
* this array is freed.
@@ -52,6 +57,7 @@ - (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FUICollecti
5257
self = [super init];
5358
if (self) {
5459
self.snapshots = [NSMutableArray array];
60+
self.keys = [NSMutableArray array];
5561
self.query = query;
5662
self.handles = [NSMutableSet setWithCapacity:4];
5763
self.delegate = delegate;
@@ -168,6 +174,7 @@ - (void)invalidate {
168174

169175
[self.snapshots removeObjectAtIndex:i];
170176

177+
[self.keys removeObjectAtIndex:i];
171178
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
172179
[self.delegate array:self didRemoveObject:current atIndex:i];
173180
}
@@ -177,13 +184,8 @@ - (void)invalidate {
177184

178185
- (NSUInteger)indexForKey:(NSString *)key {
179186
NSParameterAssert(key != nil);
180-
181-
for (NSUInteger index = 0; index < [self.snapshots count]; index++) {
182-
if ([key isEqualToString:[(FIRDataSnapshot *)[self.snapshots objectAtIndex:index] key]]) {
183-
return index;
184-
}
185-
}
186-
return NSNotFound;
187+
188+
return [self.keys indexOfObject:key];
187189
}
188190

189191
- (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)previous {
@@ -204,6 +206,7 @@ - (void)insertSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
204206
}
205207

206208
[self.snapshots insertObject:snap atIndex:index];
209+
[self.keys insertObject:snap.key atIndex:index];
207210

208211
if ([self.delegate respondsToSelector:@selector(array:didAddObject:atIndex:)]) {
209212
[self.delegate array:self didAddObject:snap atIndex:index];
@@ -223,6 +226,7 @@ - (void)removeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
223226
}
224227

225228
[self.snapshots removeObjectAtIndex:index];
229+
[self.keys removeObjectAtIndex:index];
226230

227231
if ([self.delegate respondsToSelector:@selector(array:didRemoveObject:atIndex:)]) {
228232
[self.delegate array:self didRemoveObject:snap atIndex:index];
@@ -242,6 +246,7 @@ - (void)changeSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)
242246
}
243247

244248
[self.snapshots replaceObjectAtIndex:index withObject:snap];
249+
[self.keys replaceObjectAtIndex:index withObject:snap.key];
245250

246251
if ([self.delegate respondsToSelector:@selector(array:didChangeObject:atIndex:)]) {
247252
[self.delegate array:self didChangeObject:snap atIndex:index];
@@ -261,6 +266,7 @@ - (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)pr
261266
}
262267

263268
[self.snapshots removeObjectAtIndex:fromIndex];
269+
[self.keys removeObjectAtIndex:fromIndex];
264270

265271
NSUInteger toIndex = 0;
266272
if (previous != nil) {
@@ -270,6 +276,7 @@ - (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)pr
270276
}
271277
}
272278
[self.snapshots insertObject:snap atIndex:toIndex];
279+
[self.keys insertObject:snap.key atIndex:toIndex];
273280

274281
if ([self.delegate respondsToSelector:@selector(array:didMoveObject:fromIndex:toIndex:)]) {
275282
[self.delegate array:self didMoveObject:snap fromIndex:fromIndex toIndex:toIndex];
@@ -278,14 +285,17 @@ - (void)moveSnapshot:(FIRDataSnapshot *)snap withPreviousChildKey:(NSString *)pr
278285

279286
- (void)removeSnapshotAtIndex:(NSUInteger)index {
280287
[self.snapshots removeObjectAtIndex:index];
288+
[self.keys removeObjectAtIndex:index];
281289
}
282290

283291
- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index {
284292
[self.snapshots insertObject:snap atIndex:index];
293+
[self.keys insertObject:snap.key atIndex:index];
285294
}
286295

287296
- (void)addSnapshot:(FIRDataSnapshot *)snap {
288297
[self.snapshots addObject:snap];
298+
[self.keys addObject:snap.key];
289299
}
290300

291301
#pragma mark - Public API methods

0 commit comments

Comments
 (0)