Skip to content

Commit 546c7bf

Browse files
committed
conform to FIRCollection
1 parent 6e75b83 commit 546c7bf

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

FirebaseDatabaseUI/FUIArray.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ - (instancetype)initWithQuery:(FIRDatabaseQuery *)query delegate:(id<FUICollecti
4747
self.query = query;
4848
self.handles = [NSMutableSet setWithCapacity:4];
4949
self.delegate = delegate;
50-
[self initListeners];
5150
}
5251
return self;
5352
}
@@ -63,14 +62,13 @@ + (instancetype)arrayWithQuery:(id<FUIDataObservable>)query {
6362
#pragma mark - Memory management methods
6463

6564
- (void)dealloc {
66-
for (NSNumber *handle in _handles) {
67-
[_query removeObserverWithHandle:handle.unsignedIntegerValue];
68-
}
65+
[self invalidate];
6966
}
7067

7168
#pragma mark - Private API methods
7269

73-
- (void)initListeners {
70+
- (void)observeQuery {
71+
if (self.handles.count == 4) { /* don't duplicate observers */ return; }
7472
FIRDatabaseHandle handle;
7573
handle = [self.query observeEventType:FIRDataEventTypeChildAdded
7674
andPreviousSiblingKeyWithBlock:^(FIRDataSnapshot *snapshot, NSString *previousChildKey) {
@@ -153,6 +151,12 @@ - (void)initListeners {
153151
[_handles addObject:@(handle)];
154152
}
155153

154+
- (void)invalidate {
155+
for (NSNumber *handle in _handles) {
156+
[_query removeObserverWithHandle:handle.unsignedIntegerValue];
157+
}
158+
}
159+
156160
- (NSUInteger)indexForKey:(NSString *)key {
157161
NSParameterAssert(key != nil);
158162

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)