UICollectionView crashes when changing contentInset before changing its dataSource

Originator:rnystrom
Number:rdar://32114814 Date Originated:10-May-2017
Status:Open Resolved:
Product:UIKit Product Version:10.3.2
Classification:Crash Reproducible:Always
 
Area:
UIKit

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 data source (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' and 'UICollectionViewContentInsetCrash  5-10-17, 3-41 PM.crash' were successfully uploaded.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!