I encountered the nightmarish performBatchUpdates during performBatchUpdates in a collection view .
The problem is mainly this: I have a lot of images in a directory on the server. I want to show thumbnails of these files in collection view . But the thumbnail must be loaded from the server asynchronously. Upon arrival, they will be inserted into the collection view using something like this:
dispatch_async(dispatch_get_main_queue(), ^{ [self.collectionView performBatchUpdates:^{ if (removedIndexes && [removedIndexes count] > 0) { [self.collectionView deleteItemsAtIndexPaths:removedIndexes]; } if (changedIndexes && [changedIndexes count] > 0) { [self.collectionView reloadItemsAtIndexPaths:changedIndexes]; } if (insertedIndexes && [insertedIndexes count] > 0) { [self.collectionView insertItemsAtIndexPaths:insertedIndexes]; } } completion:nil]; });
the problem is this (i think). Assume that at time 0, the collection view contains 10 elements. Then I add another 100 files to the server. The application will see new files and start downloading thumbnails. As the thumbnails load, they will be inserted into the collection view. But since loading may take different times, and this loading operation is asynchronous , at some point iOS will lose track of the number of items in the collection, and all this will fail with this catastrophic, infamous message.
*** The application terminated due to an unhandled exception "NSInternalInconsistencyException", reason: "Invalid update: invalid number of elements in section 0. The number of elements contained in an existing section after the update (213) must be equal to the number of elements contained in this section before the update (154), plus or minus the number of elements inserted or deleted from this section (40 inserted, 0 deleted) and plus or minus the number of elements moved to or from this section (0 moved to 0 moved).
The proof that I have something suspicious is that if I print out the number of elements in the data set, I see exactly 213. Thus, the data set corresponds to the correct number, and the message is meaningless.
I had this problem before, here, but it was an iOS 7 project. Somehow the problem returned now on iOS 8, and the solutions do not work there, and now the data set is in sync mode.