fix crash on missing cell

Summary: If we apply the data updates outside the `performBatchUpdates`'s `updates` block, we need to make sure the layout is up to date. Otherwise, `performBatchUpdates` will try to do a layout before calling `updates` and get the wrong cells. Calling `[self.collectionView numberOfSections]` will update the section/item count, but won't get the cells.

Reviewed By: Haud

Differential Revision: D23604257

fbshipit-source-id: 02bbf0484c46cf6e7a12ed02d45ededb3b84ac19
This commit is contained in:
Maxime Ollivier 2020-09-09 12:38:58 -07:00 committed by Facebook GitHub Bot
parent a64f9b809d
commit 02ffb1163b

View file

@ -172,8 +172,11 @@ willPerformBatchUpdatesWithCollectionView:self.collectionView
// `performBatchUpdates` right after.
const BOOL skipPerformUpdateIfPossible = IGListExperimentEnabled(self.config.experiments, IGListExperimentSkipPerformUpdateIfPossible);
if (skipPerformUpdateIfPossible) {
// Force the `UICollectionView` data to sync before applying any new updates.
[self.collectionView numberOfSections];
// From Apple docs: If the collection view's layout is not up to date before you call performBatchUpdates, a reload may
// occur. To avoid problems, you should update your data model inside the updates block or ensure the layout is
// updated before you call performBatchUpdates(_:completion:).
[self.collectionView layoutIfNeeded];
[self _applyDataUpdates];
if (!diffResult.hasChanges && !self.inUpdateItemCollector.hasChanges) {