Safely check the sectionIndex to be within the right bound before converting to use convertSectionReloadToItemUpdates

Summary:
This made the items reload safer, as I tested that passing in an out-of-bound section index to -[UICollectionView numberOfItemsInSection:] would crash the app.

UIKit did not handle this gracefully.

Reviewed By: calimarkus

Differential Revision: D9624061

fbshipit-source-id: bcf14a3d4746b648046992adf221faff36a0257c
This commit is contained in:
Zhisheng Huang 2018-08-31 18:30:46 -07:00 committed by Facebook Github Bot
parent 66742f2309
commit eddb0a5f21

View file

@ -330,7 +330,9 @@ static NSArray<NSIndexPath *> *convertSectionReloadToItemUpdates(NSIndexSet *sec
if (self.preferItemReloadsForSectionReloads) {
[reloads enumerateIndexesUsingBlock:^(NSUInteger sectionIndex, BOOL * _Nonnull stop) {
NSMutableIndexSet *localIndexSet = [NSMutableIndexSet indexSetWithIndex:sectionIndex];
if ([collectionView numberOfItemsInSection:sectionIndex] == [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:sectionIndex]) {
if (sectionIndex < [collectionView numberOfSections]
&& sectionIndex < [collectionView.dataSource numberOfSectionsInCollectionView:collectionView]
&& [collectionView numberOfItemsInSection:sectionIndex] == [collectionView.dataSource collectionView:collectionView numberOfItemsInSection:sectionIndex]) {
// Perfer to do item reloads instead, if the number of items in section is unchanged.
[itemUpdates addObjectsFromArray:convertSectionReloadToItemUpdates(localIndexSet, collectionView)];
} else {