Skip to content

Commit

Permalink
Merge pull request #610 from mastohhh/master
Browse files Browse the repository at this point in the history
optimize FUIArray’s indexForKey method by using an array of keys
  • Loading branch information
morganchen12 authored Feb 5, 2019
2 parents 24bbf32 + 6e01687 commit e135f50
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Database/FirebaseDatabaseUI/FUIArray.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ @interface FUIArray ()
*/
@property (strong, nonatomic) NSMutableArray<FIRDataSnapshot *> *snapshots;

/**
* The backing collection that holds all of the array's key.
*/
@property (strong, nonatomic) NSMutableArray<NSString *> *keys;

/**
* A set containing the query observer handles that should be released when
* this array is freed.
Expand All @@ -52,6 +57,7 @@ - (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FUICollecti
self = [super init];
if (self) {
self.snapshots = [NSMutableArray array];
self.keys = [NSMutableArray array];
self.query = query;
self.handles = [NSMutableSet setWithCapacity:4];
self.delegate = delegate;
Expand Down Expand Up @@ -168,6 +174,7 @@ - (void)invalidate {

[self.snapshots removeObjectAtIndex:i];

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

- (NSUInteger)indexForKey:(NSString *)key {
NSParameterAssert(key != nil);

for (NSUInteger index = 0; index < [self.snapshots count]; index++) {
if ([key isEqualToString:[(FIRDataSnapshot *)[self.snapshots objectAtIndex:index] key]]) {
return index;
}
}
return NSNotFound;

return [self.keys indexOfObject:key];
}

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

[self.snapshots insertObject:snap atIndex:index];
[self.keys insertObject:snap.key atIndex:index];

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

[self.snapshots removeObjectAtIndex:index];
[self.keys removeObjectAtIndex:index];

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

[self.snapshots replaceObjectAtIndex:index withObject:snap];
[self.keys replaceObjectAtIndex:index withObject:snap.key];

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

[self.snapshots removeObjectAtIndex:fromIndex];
[self.keys removeObjectAtIndex:fromIndex];

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

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

- (void)removeSnapshotAtIndex:(NSUInteger)index {
[self.snapshots removeObjectAtIndex:index];
[self.keys removeObjectAtIndex:index];
}

- (void)insertSnapshot:(FIRDataSnapshot *)snap atIndex:(NSUInteger)index {
[self.snapshots insertObject:snap atIndex:index];
[self.keys insertObject:snap.key atIndex:index];
}

- (void)addSnapshot:(FIRDataSnapshot *)snap {
[self.snapshots addObject:snap];
[self.keys addObject:snap.key];
}

#pragma mark - Public API methods
Expand Down

0 comments on commit e135f50

Please sign in to comment.