Skip to content

Instantly share code, notes, and snippets.

@radianttap
Created February 19, 2018 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radianttap/c06445d8cd24ed636a4f12fe5370a0c5 to your computer and use it in GitHub Desktop.
Save radianttap/c06445d8cd24ed636a4f12fe5370a0c5 to your computer and use it in GitHub Desktop.
Why the hell this crashes in UICollectionView?
let expandedSections = [0, 3, 8]
let indexSet = IndexSet(expandedSections)
collectionView?.performBatchUpdates(
{
[unowned self] in
if isInserted {
self.collectionView?.insertItems(at: indexPaths)
} else {
self.collectionView?.deleteItems(at: indexPaths)
}
if !indexSet.isEmpty {
self.collectionView?.reloadSections(indexSet)
}
}, completion: {
_ in
}
@radianttap
Copy link
Author

Crash is this:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** 
-[NSMutableIndexSet addIndexesInRange:]: Range {9223372036854775807, 1} exceeds maximum index value of NSNotFound - 1'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000011000a12b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x000000010ed0cf41 objc_exception_throw + 48
	2   CoreFoundation                      0x000000011007f245 +[NSException raise:format:] + 197
	3   Foundation                          0x000000010bfe0309 -[NSMutableIndexSet addIndexesInRange:] + 110
	4   UIKit                               0x000000010d580613 -[UICollectionViewUpdate _computeSupplementaryUpdates] + 1820
	5   UIKit                               0x000000010d614f96 -[UICollectionView _updateWithItems:tentativelyForReordering:animator:] + 3535
	6   UIKit                               0x000000010d60f4c2 -[UICollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:animator:] + 17338
	7   UIKit                               0x000000010d617ac1 -[UICollectionView _endUpdatesWithInvalidationContext:tentativelyForReordering:animator:] + 71
	8   UIKit                               0x000000010d617e06 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:animator:] + 435
	9   UIKit                               0x000000010d617c30 -[UICollectionView _performBatchUpdates:completion:invalidationContext:tentativelyForReordering:] + 91
	10  UIKit                               0x000000010d617bb2 -[UICollectionView _performBatchUpdates:completion:invalidationContext:] + 74
	11  UIKit                               0x000000010d617b07 -[UICollectionView performBatchUpdates:completion:] + 53

Where did it find NSNotFound..?!

@radianttap
Copy link
Author

2 years later, I have the answer. Happens when you do this:

let indexPath = IndexPath(item: NSNotFound, section: section)
let attr = UICollectionViewLayoutAttributes(forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, with: indexPath)

and somewhere along the way you do collectionView.reloadSections()

Problem is indexPath, which should be:

let indexPath = IndexPath(item: 0, section: section)

@ivanglushko
Copy link

ivanglushko commented Jul 7, 2021

Hi I'm facing the same issue using framework for CollectionView well that is weird.
Thanks for posting the reason why it happens.
Although i did not understand fully why this is happening.
My only hint is that somehow indexSet is over-floating.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment