Skip to content

Commit 0b1fd5c

Browse files
authored
Merge pull request #239 from morganchen12/datasource-refactor
fix data source init having side effects
2 parents 50d8bb6 + 4588fa0 commit 0b1fd5c

File tree

11 files changed

+108
-200
lines changed

11 files changed

+108
-200
lines changed

FirebaseDatabaseUI/FUICollection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
@import Foundation;
2222

23-
@class FUIArray;
23+
@class FIRDatabaseReference, FIRDatabaseQuery, FIRDataSnapshot;
2424
@protocol FUICollectionDelegate;
2525

2626
NS_ASSUME_NONNULL_BEGIN

FirebaseDatabaseUI/FUICollectionViewDataSource.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,17 @@
2020

2121
@import UIKit;
2222

23-
#import "FUIDataSource.h"
23+
#import <FirebaseDatabaseUI/FUICollection.h>
2424

2525
NS_ASSUME_NONNULL_BEGIN
2626

27-
@class FIRDatabaseReference;
28-
2927
/**
3028
* FUICollectionViewDataSource provides a class that conforms to the
3129
* UICollectionViewDataSource protocol which allows UICollectionViews to
3230
* adopt FUICollectionViewDataSource in order to provide a UICollectionView
3331
* synchronized to a Firebase reference or query.
3432
*/
35-
@interface FUICollectionViewDataSource : FUIDataSource<UICollectionViewDataSource>
33+
@interface FUICollectionViewDataSource : NSObject <UICollectionViewDataSource>
3634

3735
/**
3836
* The UICollectionView instance that operations (inserts, removals, moves,
@@ -46,16 +44,19 @@ NS_ASSUME_NONNULL_BEGIN
4644
* The callback to populate a subclass of UICollectionViewCell with an object
4745
* provided by the datasource.
4846
*/
49-
@property(strong, nonatomic, readonly) UICollectionViewCell *(^populateCellAtIndexPath)
47+
@property (strong, nonatomic, readonly) UICollectionViewCell *(^populateCellAtIndexPath)
5048
(UICollectionView *collectionView, NSIndexPath *indexPath, FIRDataSnapshot *object);
5149

50+
/**
51+
* The number of items in the data source.
52+
*/
53+
@property (nonatomic, readonly) NSUInteger count;
54+
5255
/**
5356
* Initialize an instance of FUICollectionViewDataSource that populates
5457
* UICollectionViewCells with FIRDataSnapshots.
5558
* @param collection A FUICollection that the data source uses to pull snapshots
5659
* from Firebase Database.
57-
* @param view An instance of a UICollectionView to bind to. This view
58-
* is not retained by its data source.
5960
* @param populateCell A closure used by the data source to create the cells that
6061
* are displayed in the collection view. This closure is retained by the data
6162
* source, so if you capture self in the closure and also claim ownership of the
@@ -64,7 +65,6 @@ NS_ASSUME_NONNULL_BEGIN
6465
* UICollectionViewCells with FIRDataSnapshots.
6566
*/
6667
- (instancetype)initWithCollection:(id<FUICollection>)collection
67-
view:(UICollectionView *)view
6868
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
6969
NSIndexPath *indexPath,
7070
FIRDataSnapshot *object))populateCell NS_DESIGNATED_INITIALIZER;
@@ -73,8 +73,6 @@ NS_ASSUME_NONNULL_BEGIN
7373
* Initialize an unsorted instance of FUICollectionViewDataSource that populates
7474
* UICollectionViewCells with FIRDataSnapshots.
7575
* @param query A Firebase query to bind the data source to.
76-
* @param collectionView An instance of a UICollectionView to bind to. This view
77-
* is not retained by its data source.
7876
* @param populateCell A closure used by the data source to create the cells that
7977
* are displayed in the collection view. This closure is retained by the data
8078
* source, so if you capture self in the closure and also claim ownership of the
@@ -83,12 +81,23 @@ NS_ASSUME_NONNULL_BEGIN
8381
* UICollectionViewCells with FIRDataSnapshots.
8482
*/
8583
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
86-
view:(UICollectionView *)collectionView
8784
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
8885
NSIndexPath *indexPath,
8986
FIRDataSnapshot *object))populateCell;
9087

91-
- (instancetype)initWithCollection:(id<FUICollection>)collection NS_UNAVAILABLE;
88+
- (instancetype)init NS_UNAVAILABLE;
89+
90+
/**
91+
* Attaches the data source to a collection view and begins sending updates immediately.
92+
* @param view An instance of UICollectionView that the data source should push
93+
* updates to.
94+
*/
95+
- (void)bindToView:(UICollectionView *)view;
96+
97+
/**
98+
* Detaches the data source from a view and stops sending any updates.
99+
*/
100+
- (void)unbind;
92101

93102
@end
94103

FirebaseDatabaseUI/FUICollectionViewDataSource.m

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,54 @@
1919
// clang-format on
2020

2121
#import "FUICollectionViewDataSource.h"
22+
#import "FUIArray.h"
2223

2324
@import FirebaseDatabase;
2425

26+
@interface FUICollectionViewDataSource ()
27+
28+
@property (nonatomic, readonly, nonnull) id<FUICollection> collection;
29+
30+
@end
31+
2532
@implementation FUICollectionViewDataSource
2633

2734
#pragma mark - FUIDataSource initializer methods
2835

2936
- (instancetype)initWithCollection:(id<FUICollection>)collection
30-
view:(UICollectionView *)view
3137
populateCell:(UICollectionViewCell * (^)(UICollectionView *,
3238
NSIndexPath *,
3339
FIRDataSnapshot *))populateCell {
34-
self = [super initWithCollection:collection];
40+
self = [super init];
3541
if (self) {
36-
_collectionView = view;
42+
_collection = collection;
3743
_populateCellAtIndexPath = populateCell;
3844
}
3945
return self;
4046
}
4147

4248
- (instancetype)initWithQuery:(FIRDatabaseQuery *)query
43-
view:(UICollectionView *)collectionView
4449
populateCell:(UICollectionViewCell *(^)(UICollectionView *,
4550
NSIndexPath *,
4651
FIRDataSnapshot *))populateCell {
4752
FUIArray *array = [[FUIArray alloc] initWithQuery:query];
48-
return [self initWithCollection:array view:collectionView populateCell:populateCell];
53+
return [self initWithCollection:array populateCell:populateCell];
54+
}
55+
56+
- (NSUInteger)count {
57+
return self.collection.count;
58+
}
59+
60+
- (void)bindToView:(UICollectionView *)view {
61+
self.collectionView = view;
62+
view.dataSource = self;
63+
[self.collection observeQuery];
64+
}
65+
66+
- (void)unbind {
67+
self.collectionView.dataSource = nil;
68+
self.collectionView = nil;
69+
[self.collection invalidate];
4970
}
5071

5172
#pragma mark - FUICollectionDelegate methods
@@ -75,7 +96,7 @@ - (void)array:(FUIArray *)array didMoveObject:(id)object
7596

7697
- (nonnull UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView
7798
cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
78-
FIRDataSnapshot *snap = [self.items objectAtIndex:indexPath.row];
99+
FIRDataSnapshot *snap = [self.collection.items objectAtIndex:indexPath.item];
79100

80101
UICollectionViewCell *cell = self.populateCellAtIndexPath(collectionView, indexPath, snap);
81102

@@ -88,7 +109,7 @@ - (NSInteger)numberOfSectionsInCollectionView:(nonnull UICollectionView *)collec
88109

89110
- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView
90111
numberOfItemsInSection:(NSInteger)section {
91-
return self.count;
112+
return self.collection.count;
92113
}
93114

94115
@end
@@ -100,8 +121,8 @@ - (FUICollectionViewDataSource *)bindToQuery:(FIRDatabaseQuery *)query
100121
NSIndexPath *,
101122
FIRDataSnapshot *))populateCell {
102123
FUICollectionViewDataSource *dataSource =
103-
[[FUICollectionViewDataSource alloc] initWithQuery:query view:self populateCell:populateCell];
104-
self.dataSource = dataSource;
124+
[[FUICollectionViewDataSource alloc] initWithQuery:query populateCell:populateCell];
125+
[dataSource bindToView:self];
105126
return dataSource;
106127
}
107128

FirebaseDatabaseUI/FUIDataSource.h

Lines changed: 0 additions & 65 deletions
This file was deleted.

FirebaseDatabaseUI/FUIDataSource.m

Lines changed: 0 additions & 74 deletions
This file was deleted.

0 commit comments

Comments
 (0)