From 02ffb1163bbd6dcbae4b33c127b47012c5c06741 Mon Sep 17 00:00:00 2001 From: Maxime Ollivier Date: Wed, 9 Sep 2020 12:38:58 -0700 Subject: [PATCH] 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 --- Source/IGListKit/Internal/IGListBatchUpdateTransaction.m | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Source/IGListKit/Internal/IGListBatchUpdateTransaction.m b/Source/IGListKit/Internal/IGListBatchUpdateTransaction.m index cbf07538..07f52eb4 100644 --- a/Source/IGListKit/Internal/IGListBatchUpdateTransaction.m +++ b/Source/IGListKit/Internal/IGListBatchUpdateTransaction.m @@ -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) {