Skip to content

Commit faa5de5

Browse files
committed
conform to FIRCollection
1 parent 07c68de commit faa5de5

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

FirebaseDatabaseUI/FUIArray.m

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,20 @@ - (instancetype)initWithQuery:(FIRDatabaseQuery *)query {
4646
self.snapshots = [NSMutableArray array];
4747
self.query = query;
4848
self.handles = [NSMutableSet setWithCapacity:4];
49-
50-
[self initListeners];
5149
}
5250
return self;
5351
}
5452

5553
#pragma mark - Memory management methods
5654

5755
- (void)dealloc {
58-
for (NSNumber *handle in _handles) {
59-
[_query removeObserverWithHandle:handle.unsignedIntegerValue];
60-
}
56+
[self invalidate];
6157
}
6258

6359
#pragma mark - Private API methods
6460

65-
- (void)initListeners {
61+
- (void)observeQuery {
62+
if (self.handles.count == 4) { /* don't duplicate observers */ return; }
6663
FIRDatabaseHandle handle;
6764
handle = [self.query observeEventType:FIRDataEventTypeChildAdded
6865
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
@@ -144,6 +141,12 @@ - (void)initListeners {
144141
[_handles addObject:@(handle)];
145142
}
146143

144+
- (void)invalidate {
145+
for (NSNumber *handle in _handles) {
146+
[_query removeObserverWithHandle:handle.unsignedIntegerValue];
147+
}
148+
}
149+
147150
- (NSUInteger)indexForKey:(NSString *)key {
148151
NSParameterAssert(key != nil);
149152

@@ -165,7 +168,7 @@ - (NSUInteger)count {
165168
return [self.snapshots count];
166169
}
167170

168-
- (FIRDataSnapshot *)objectAtIndex:(NSUInteger)index {
171+
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index {
169172
return (FIRDataSnapshot *)[self.snapshots objectAtIndex:index];
170173
}
171174

@@ -174,7 +177,7 @@ - (FIRDatabaseReference *)refForIndex:(NSUInteger)index {
174177
}
175178

176179
- (id)objectAtIndexedSubscript:(NSUInteger)index {
177-
return [self objectAtIndex:index];
180+
return [self snapshotAtIndex:index];
178181
}
179182

180183
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index{

FirebaseDatabaseUI/FUICollection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ NS_ASSUME_NONNULL_BEGIN
3838

3939
- (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index;
4040

41+
- (void)observeQuery;
42+
- (void)invalidate;
43+
4144
@end
4245

4346
/**

FirebaseDatabaseUI/FUIDataSource.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ - (instancetype)initWithArray:(FUIArray *)array {
4040
if (self) {
4141
_array = array;
4242
_array.delegate = self;
43+
[array observeQuery];
4344
}
4445
return self;
4546
}

FirebaseDatabaseUI/FUISortedArray.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ - (void)dealloc {
6363
}
6464

6565
- (void)observeQuery {
66+
if (self.handles.count == 4) { /* don't duplicate observers */ return; }
6667
FIRDatabaseHandle handle;
6768
handle = [self.query observeEventType:FIRDataEventTypeChildAdded
6869
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {

0 commit comments

Comments
 (0)