2121@interface FUIIndexCollectionViewDataSource () <FUIIndexArrayDelegate>
2222
2323@property (nonatomic , readonly , nonnull ) FUIIndexArray *array;
24- @property (nonatomic , readonly , weak ) UICollectionView *collectionView;
24+ @property (nonatomic , weak , nullable ) UICollectionView *collectionView;
2525
2626@property (nonatomic , readonly , copy ) UICollectionViewCell *(^populateCell)
2727 (UICollectionView *collectionView, NSIndexPath *indexPath, FIRDataSnapshot *object);
@@ -32,17 +32,24 @@ @implementation FUIIndexCollectionViewDataSource
3232
3333- (instancetype )initWithIndex : (FIRDatabaseQuery *)indexQuery
3434 data : (FIRDatabaseReference *)dataQuery
35- collectionView : (UICollectionView *)collectionView
35+ delegate : (id <FUIIndexCollectionViewDataSourceDelegate>)delegate
36+ populateCell : (UICollectionViewCell *(^)(UICollectionView *collectionView,
37+ NSIndexPath *indexPath,
38+ FIRDataSnapshot *snap))populateCell {
39+ FUIIndexArray *array = [[FUIIndexArray alloc ] initWithIndex: indexQuery
40+ data: dataQuery
41+ delegate: self ];
42+ return [self initWithIndexArray: array delegate: delegate populateCell: populateCell];
43+ }
44+
45+ - (instancetype )initWithIndexArray : (FUIIndexArray *)indexArray
3646 delegate : (id <FUIIndexCollectionViewDataSourceDelegate>)delegate
3747 populateCell : (UICollectionViewCell *(^)(UICollectionView *collectionView,
3848 NSIndexPath *indexPath,
3949 FIRDataSnapshot *snap))populateCell {
4050 self = [super init ];
4151 if (self != nil ) {
42- _array = [[FUIIndexArray alloc ] initWithIndex: indexQuery
43- data: dataQuery
44- delegate: self ];
45- _collectionView = collectionView;
52+ _array = indexArray;
4653 _collectionView.dataSource = self;
4754 _populateCell = populateCell;
4855 _delegate = delegate;
@@ -58,6 +65,18 @@ - (FIRDataSnapshot *)snapshotAtIndex:(NSInteger)index {
5865 return [self .array objectAtIndex: index];
5966}
6067
68+ - (void )bindToView : (UICollectionView *)view {
69+ self.collectionView = view;
70+ view.dataSource = self;
71+ [self .array observeQuery ];
72+ }
73+
74+ - (void )unbind {
75+ [self .array invalidate ];
76+ self.collectionView .dataSource = nil ;
77+ self.collectionView = nil ;
78+ }
79+
6180#pragma mark - FUIIndexArrayDelegate
6281
6382- (void )array : (FUIIndexArray *)array
@@ -115,7 +134,8 @@ - (void)array:(FUIIndexArray *)array
115134
116135#pragma mark - UICollectionViewDataSource
117136
118- - (NSInteger )collectionView : (UICollectionView *)collectionView numberOfItemsInSection : (NSInteger )section {
137+ - (NSInteger )collectionView : (UICollectionView *)collectionView
138+ numberOfItemsInSection : (NSInteger )section {
119139 return self.array .count ;
120140}
121141
@@ -131,18 +151,17 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
131151@implementation UICollectionView (FUIIndexCollectionViewDataSource)
132152
133153- (FUIIndexCollectionViewDataSource *)bindToIndexedQuery : (FIRDatabaseQuery *)index
134- data : (FIRDatabaseReference *)data
135- delegate : (id <FUIIndexCollectionViewDataSourceDelegate>)delegate
136- populateCell : (UICollectionViewCell *(^)(UICollectionView *,
137- NSIndexPath *,
138- FIRDataSnapshot *))populateCell {
154+ data : (FIRDatabaseReference *)data
155+ delegate : (id <FUIIndexCollectionViewDataSourceDelegate>)delegate
156+ populateCell : (UICollectionViewCell *(^)(UICollectionView *,
157+ NSIndexPath *,
158+ FIRDataSnapshot *))populateCell {
139159 FUIIndexCollectionViewDataSource *dataSource =
140160 [[FUIIndexCollectionViewDataSource alloc ] initWithIndex: index
141- data: data
142- collectionView: self
143- delegate: delegate
144- populateCell: populateCell];
145- self.dataSource = dataSource;
161+ data: data
162+ delegate: delegate
163+ populateCell: populateCell];
164+ [dataSource bindToView: self ];
146165 return dataSource;
147166}
148167
0 commit comments