Skip to content

Commit

Permalink
add assert for iOS 18 supplementary view dequeue exceptions
Browse files Browse the repository at this point in the history
Summary: Same as last diff, but for supplementary views!

Differential Revision: D62813794

fbshipit-source-id: f1630fd49740b1b34b422a0b4fd3055a3f0f0b21
  • Loading branch information
Maxime Ollivier authored and facebook-github-bot committed Sep 17, 2024
1 parent ab94446 commit 4bad7d5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
21 changes: 18 additions & 3 deletions Source/IGListKit/IGListAdapter.m
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N
[self.registeredSupplementaryViewIdentifiers addObject:identifier];
[collectionView registerClass:viewClass forSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier];
}
return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
return [self _dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath forSectionController:sectionController];
}

- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewFromStoryboardOfKind:(NSString *)elementKind
Expand All @@ -1238,7 +1238,7 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewFromStory
UICollectionView *collectionView = self.collectionView;
IGAssert(collectionView != nil, @"Dequeueing Supplementary View from storyboard of kind %@ with identifier %@ for section controller %@ without a collection view at index %li", elementKind, identifier, sectionController, (long)index);
NSIndexPath *indexPath = [self indexPathForSectionController:sectionController index:index usePreviousIfInUpdateBlock:NO];
return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
return [self _dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath forSectionController:sectionController];
}

- (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
Expand All @@ -1257,7 +1257,22 @@ - (__kindof UICollectionReusableView *)dequeueReusableSupplementaryViewOfKind:(N
UINib *nib = [UINib nibWithNibName:nibName bundle:bundle];
[collectionView registerNib:nib forSupplementaryViewOfKind:elementKind withReuseIdentifier:nibName];
}
return [collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:nibName forIndexPath:indexPath];
return [self _dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:nibName forIndexPath:indexPath forSectionController:sectionController];
}

- (__kindof UICollectionReusableView *)_dequeueReusableSupplementaryViewOfKind:(NSString *)elementKind
withReuseIdentifier:(NSString *)identifier
forIndexPath:(NSIndexPath *)indexPath
forSectionController:(IGListSectionController *)sectionController {
// These will cause a crash in iOS 18
IGAssert(_dequeuedSupplementaryViews.count == 0, @"Dequeueing more than one supplementary-view (%@) for indexPath %@, section controller %@,", identifier, indexPath, sectionController);
IGAssert(_isDequeuingSupplementaryView, @"Dequeueing a supplementary-view (%@) without a request from the UICollectionView for indexPath %@, section controller %@", identifier, indexPath, sectionController);

UICollectionReusableView *const view = [self.collectionView dequeueReusableSupplementaryViewOfKind:elementKind withReuseIdentifier:identifier forIndexPath:indexPath];
if (_isDequeuingSupplementaryView && view) {
[_dequeuedSupplementaryViews addObject:view];
}
return view;
}

- (void)performBatchAnimated:(BOOL)animated updates:(void (^)(id<IGListBatchContext>))updates completion:(void (^)(BOOL))completion {
Expand Down
12 changes: 12 additions & 0 deletions Source/IGListKit/Internal/IGListAdapter+UICollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,25 @@ - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cell
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
IGListSectionController *sectionController = [self sectionControllerForSection:indexPath.section];
id <IGListSupplementaryViewSource> supplementarySource = [sectionController supplementaryViewSource];

#if IG_ASSERTIONS_ENABLED
if (!_dequeuedSupplementaryViews) {
_dequeuedSupplementaryViews = [NSMutableSet new];
}
#endif

// flag that a supplementary view is being dequeued in case it tries to access a supplementary view in the process
_isDequeuingSupplementaryView = YES;
UICollectionReusableView *view = [supplementarySource viewForSupplementaryElementOfKind:kind atIndex:indexPath.item];
_isDequeuingSupplementaryView = NO;

IGAssert(view != nil, @"Returned a nil supplementary view at indexPath <%@> from section controller: <%@>, supplementary source: <%@>", indexPath, sectionController, supplementarySource);

if (view && _dequeuedSupplementaryViews) {
// This will cause a crash in iOS 18
IGAssert([_dequeuedSupplementaryViews containsObject:view], @"Returned a supplementary-view (%@) that was not dequeued at indexPath %@ from supplementary source %@", NSStringFromClass([view class]), indexPath, supplementarySource);
}
[_dequeuedSupplementaryViews removeAllObjects];

// associate the section controller with the cell so that we know which section controller is using it
[self mapView:view toSectionController:sectionController];
Expand Down
1 change: 1 addition & 0 deletions Source/IGListKit/Internal/IGListAdapterInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ IGListBatchContext
NSMutableSet<UICollectionViewCell *> *_dequeuedCells;

BOOL _isDequeuingSupplementaryView;
NSMutableSet<UICollectionReusableView *> *_dequeuedSupplementaryViews;

BOOL _isSendingWorkingRangeDisplayUpdates;
}
Expand Down

0 comments on commit 4bad7d5

Please sign in to comment.