Instagram / IGListKit

A data-driven UICollectionView framework for building fast and flexible lists.
https://instagram.github.io/IGListKit/
MIT License
12.87k stars 1.54k forks source link

[AppleBug] UICollectionView crashes when changing contentInset before changing its dataSource #732

Closed rnystrom closed 5 years ago

rnystrom commented 7 years ago

Summary: When using "embedded" UICollectionViews and UICollectionViewFlowLayout (e.g. a vertical, full-width collection view that has cells with horizontal UICollectionViews in them), if you change the contentInset property of one of the embedded collection views before changing the dataSource (changed because the cells are reused), UICollectionView will throw when validating layout attributes.

This seems to be because changing the contentInset triggers an invalidation pass, setting some internal state that disallows another invalidation after changing the dataSource (which should trigger an internal reloadData and invalidation).

This issue can be avoided by using a custom UICollectionViewFlowLayout subclass and overriding:

- (BOOL)invalidateFlowLayoutDelegateMetrics {
  return YES;
}

But that is both undocumented and unexpected behavior.

This also only occurs when changing the contentInset.top between reused collection views.

Steps to Reproduce:

  1. Open uploaded example app.
  2. Scroll to bottom.
  3. Crash (100% of the time)

Expected Results: UICollectionView shouldn't throw an exception, the views should be laid out with the correct contentInset, and the dataSource change should result in a reloadData call that invalidates on layoutSubviews.

Actual Results: UICollectionView crashes, throwing an exception like:

'NSInternalInconsistencyException', reason: 'UICollectionView received layout attributes for a cell with an index path that does not exist:

Version: iOS 10.3.2

Notes:

Configuration: iPhone 7+, iPhone 7 Simulator

Attachments: UICollectionViewContentInsetCrash.zip

rnystrom commented 7 years ago

Please dupe

https://openradar.appspot.com/radar?id=5041489335812096

jessesquires commented 7 years ago
  1. Crash (100% of the time)

😆

bdotdub commented 5 years ago

Just ran the project and confirmed it no longer happens. Closing this out!