iOS 9: UICollectionView dataSource is not set

2019-04-27 11:56发布

问题:

Since iOS 9 launched, crashalytics has reported a fair number of crashes from NSInternalInconsistencyException: UICollectionView dataSource is not set even when the data source is set. It feels like it might be a SDK bug, but I have no idea how to avoid it.

Details:

  • This only happens on iOS 9. Hundreds of occurrences on iOS 9, zero on 7/8
  • This happens on the main thread, deep in UIKit code
  • I am setting the dataSource, but it looks like the UICollectionView is outliving the datasource?
  • No consistent repro, but it happens many times a day according to crashalytics.
Thread : Fatal Exception: NSInternalInconsistencyException
0  CoreFoundation                 0x181a91900 __exceptionPreprocess
1  libobjc.A.dylib                0x1810fff80 objc_exception_throw
2  CoreFoundation                 0x181a917d0 +[NSException raise:format:]
3  Foundation                     0x18240499c -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:]
4  UIKit                          0x1869152dc -[UICollectionView _createPreparedSupplementaryViewForElementOfKind:atIndexPath:withLayoutAttributes:applyAttributes:]
5  UIKit                          0x1867e8990 -[UICollectionView _updateVisibleCellsNow:]
6  UIKit                          0x1867e389c -[UICollectionView layoutSubviews]