Skip to content

Commit a17a8db

Browse files
committed
fix bug where some reloads wouldn't be performed
Change-Id: I4482996ea218f6c9b2054cf00e4a3f8757da19ba
1 parent 672cb89 commit a17a8db

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

FirebaseFirestoreUI/FUIBatchedArray.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ - (BOOL)isEqual:(FIRDocumentSnapshot *)object {
128128
return NO;
129129
}
130130

131-
return [object.documentID isEqual:self.documentID] && [object.data isEqual:self.data];
131+
return [object.documentID isEqual:self.documentID];
132132
}
133133

134134
- (instancetype)copyWithZone:(NSZone *)zone {

FirebaseFirestoreUI/FUICollectionViewDataSource.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ NS_ASSUME_NONNULL_BEGIN
2828
* FUICollectionViewDataSource provides a class that conforms to the
2929
* UICollectionViewDataSource protocol which allows UICollectionViews to
3030
* adopt FUICollectionViewDataSource in order to provide a UICollectionView
31-
* synchronized to a Firebase reference or query.
31+
* synchronized to a Firestore reference or query.
3232
*/
3333
@interface FUICollectionViewDataSource : NSObject <UICollectionViewDataSource>
3434

@@ -74,7 +74,7 @@ NS_ASSUME_NONNULL_BEGIN
7474
* Initialize an instance of FUICollectionViewDataSource that populates
7575
* UICollectionViewCells with FIRDataSnapshots.
7676
* @param collection A FUICollection that the data source uses to pull snapshots
77-
* from Firebase Database.
77+
* from Cloud Firestore.
7878
* @param populateCell A closure used by the data source to create the cells that
7979
* are displayed in the collection view. This closure is retained by the data
8080
* source, so if you capture self in the closure and also claim ownership of the
@@ -90,7 +90,7 @@ NS_ASSUME_NONNULL_BEGIN
9090
/**
9191
* Initialize an unsorted instance of FUICollectionViewDataSource that populates
9292
* UICollectionViewCells with FIRDataSnapshots.
93-
* @param query A Firebase query to bind the data source to.
93+
* @param query A Firestore query to bind the data source to.
9494
* @param populateCell A closure used by the data source to create the cells that
9595
* are displayed in the collection view. This closure is retained by the data
9696
* source, so if you capture self in the closure and also claim ownership of the
@@ -125,14 +125,14 @@ NS_ASSUME_NONNULL_BEGIN
125125
* Creates a data source, attaches it to the collection view, and returns it.
126126
* The returned data source is not retained by the collection view and must be
127127
* retained or it will be deallocated while still in use by the collection view.
128-
* @param query A Firebase database query to bind the collection view to.
128+
* @param query A Cloud Firestore query to bind the collection view to.
129129
* @param populateCell A closure used by the data source to create the cells
130130
* displayed in the collection view. The closure is retained by the returned
131131
* data source.
132132
* @return The created data source. This value must be retained while the collection
133133
* view is in use.
134134
*/
135-
- (FUICollectionViewDataSource *)bindToQuery:(FIRDocumentSnapshot *)query
135+
- (FUICollectionViewDataSource *)bindToQuery:(FIRQuery *)query
136136
populateCell:(UICollectionViewCell *(^)(UICollectionView *collectionView,
137137
NSIndexPath *indexPath,
138138
FIRDocumentSnapshot *object))populateCell __attribute__((warn_unused_result));

FirebaseFirestoreUI/FUICollectionViewDataSource.m

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ - (void)batchedArray:(FUIBatchedArray *)array
110110
NSIndexPath *changed = [NSIndexPath indexPathForItem:changedIndex.integerValue inSection:0];
111111
[changedIndexPaths addObject:changed];
112112
}
113-
[self.collectionView reloadItemsAtIndexPaths:changedIndexPaths];
113+
// Use a delete and insert instead of a reload. See
114+
// https://stackoverflow.com/questions/42147822/uicollectionview-batchupdate-edge-case-fails
115+
[self.collectionView deleteItemsAtIndexPaths:changedIndexPaths];
116+
[self.collectionView insertItemsAtIndexPaths:changedIndexPaths];
114117

115118
for (NSInteger i = 0; i < diff.movedInitialIndexes.count; i++) {
116119
NSInteger initialIndex = diff.movedInitialIndexes[i].integerValue;

FirebaseFirestoreUI/FUISnapshotArrayDiff.m

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,11 +416,20 @@ - (void)buildDiffsFromDocumentChanges:(NSArray<FIRDocumentChange *> *)documentCh
416416
}
417417
continue;
418418
case FIRDocumentChangeTypeModified:
419-
// Don't try to reload changes that were later deleted.
420-
if (newIndex == nil) { continue; }
421-
if (newIndex != oldIndex) { continue; /* Not a change */ }
422-
[changedIndexes addObject:newIndex];
423-
[changedObjects addObject:snapshot];
419+
// Don't try to reload changes that weren't in the initial and result arrays.
420+
if (newIndex == nil || oldIndex == nil) { continue; }
421+
// This should be counted as a move.
422+
if (![newIndex isEqualToNumber:oldIndex]) {
423+
[movedInitialIndexes addObject:oldIndex];
424+
[movedResultIndexes addObject:newIndex];
425+
[movedObjects addObject:snapshot];
426+
427+
// Keep track of which insertions we should ignore later.
428+
[movedSnapshots addObject:snapshot];
429+
} else {
430+
[changedIndexes addObject:oldIndex];
431+
[changedObjects addObject:snapshot];
432+
}
424433
continue;
425434
case FIRDocumentChangeTypeAdded:
426435
// Ignore insertions that were later removed.
@@ -429,10 +438,9 @@ - (void)buildDiffsFromDocumentChanges:(NSArray<FIRDocumentChange *> *)documentCh
429438
// counted as moves.
430439
if (oldIndex != nil) { continue; }
431440
// Ignore insertions that we consider moves.
432-
if (![movedSnapshots containsObject:snapshot]) {
433-
[insertedIndexes addObject:newIndex];
434-
[insertedObjects addObject:snapshot];
435-
}
441+
if ([movedSnapshots containsObject:snapshot]) { continue; }
442+
[insertedIndexes addObject:newIndex];
443+
[insertedObjects addObject:snapshot];
436444
continue;
437445
}
438446
}

FirebaseFirestoreUI/FUITableViewDataSource.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@
2424

2525
NS_ASSUME_NONNULL_BEGIN
2626

27-
@class FIRDatabaseReference;
28-
2927
/**
3028
* FUITableViewDataSource provides a class that conforms to the
3129
* UITableViewDataSource protocol which allows UITableViews to implement
3230
* FUITableViewDataSource in order to provide a UITableView synchronized
33-
* to a Firebase reference or query.
31+
* to a Firestore reference or query.
3432
*/
3533
@interface FUITableViewDataSource : NSObject <UITableViewDataSource>
3634

@@ -80,7 +78,7 @@ NS_ASSUME_NONNULL_BEGIN
8078
/**
8179
* Initialize an instance of FUITableViewDataSource.
8280
* @param collection An FUICollection used by the data source to pull data
83-
* from Firebase Database.
81+
* from Cloud Firestore.
8482
* @param populateCell A closure used by the data source to create/reuse
8583
* table view cells and populate their content. This closure is retained
8684
* by the data source, so if you capture self in the closure and also claim ownership
@@ -96,7 +94,7 @@ NS_ASSUME_NONNULL_BEGIN
9694
/**
9795
* Initialize an instance of FUITableViewDataSource with contents ordered
9896
* by the query.
99-
* @param query A Firebase query to bind the data source to.
97+
* @param query A Firestore query to bind the data source to.
10098
* @param populateCell A closure used by the data source to create/reuse
10199
* table view cells and populate their content. This closure is retained
102100
* by the data source, so if you capture self in the closure and also claim ownership
@@ -130,7 +128,7 @@ NS_ASSUME_NONNULL_BEGIN
130128
* Creates a data source, attaches it to the table view, and returns it.
131129
* The returned data source is not retained by the table view and must be
132130
* retained or it will be deallocated while still in use by the table view.
133-
* @param query A Firebase database query to bind the table view to.
131+
* @param query A Cloud Firestore query to bind the table view to.
134132
* @param populateCell A closure used by the data source to create the cells
135133
* displayed in the table view. The closure is retained by the returned
136134
* data source.

0 commit comments

Comments
 (0)