What’s causing this iOS crash? UICollectionView received layout attributes for a cell with an index path that does not exist

The crash was happening in the following situation:

We had a collection view controller that was presenting another view controller on top of it.

While the collection view controller was no longer visible, the following sequence of events was occasionally occurring in response to our app’s back end requests.

  1. [UICollectionView insertItemsAtIndexPaths:] was called with 50
    items on the collection view of the hidden UICollectionViewController.
  2. [UICollectionView reloadData] was called on the hidden collection view.
  3. A short delay would occur.
  4. The number of items in the hidden collection view was set to a small number.
  5. [UICollectionView reloadData] was called again.
  6. The view controller was dismissed, revealing the hidden collection view controller.

The assertion failure in the internal UIKit class UICollectionViewData would happen at step 6.

So, the lesson is, try to avoid manipulating a collection view that is not visible on the screen.

Our workaround for this problem was to call [UICollectionView reloadSections:] instead of [UICollectionView reloadData] at key points.

We suspect that the effects of reloadData are deferred to some point in the future, and there are consequently subtle issues with how this may interact with other method calls like insertItemsAtIndexPaths, whereas reloadSections is handled immediately, leaving the collection view in a better state.

We think that we were not seeing this behavior until we started building our app for iOS 8.

Sleep well, my friends!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)