Nightmare with performBatchUpdates crash
<p>I am facing a nightmare of a crash during <code>performBatchUpdates</code> on a <code>collection view</code>.</p>

<p>The problem is basically this: I have a lot of images on a directory on a server. I want to show the <em>thumbnails</em> of those files on a <code>collection view</code>. But the thumbnail have to be downloaded from the server <em>asynchronously</em>. As they arrive they will be inserted on the collection view using something like this:</p>

<pre><code>dispatch_async(dispatch_get_main_queue(),
             ^{
               [self.collectionView performBatchUpdates:^{

                 if (removedIndexes &amp;&amp; [removedIndexes count] &gt; 0) {
                   [self.collectionView deleteItemsAtIndexPaths:removedIndexes];
                 }

                 if (changedIndexes &amp;&amp; [changedIndexes count] &gt; 0) {
                   [self.collectionView reloadItemsAtIndexPaths:changedIndexes];
                 }

                 if (insertedIndexes &amp;&amp; [insertedIndexes count] &gt; 0) {
                   [self.collectionView insertItemsAtIndexPaths:insertedIndexes];
                 }

               } completion:nil];
             });
</code></pre>

<p>the problem is this (I think). Suppose that at time = 0, the collection view has 10 items. I then add 100 more files to the server. The application sees the new files and start downloading the thumbnails. As the thumbnails download they will be inserted on the collection view. But because the downloads can take different times and this download operation is <code>asynchronous</code>, at one point iOS will lost track of how many elements the collection has and the whole thing will crash with this catastrophic infamous message.</p>

<blockquote>
  <p>*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid
  number of items in section 0.  The number of items contained in an
  existing section after the update (213) must be equal to the number of
  items contained in that section before the update (154), plus or minus
  the number of items inserted or deleted from that section (40
  inserted, 0 deleted) and plus or minus the number of items moved into
  or out of that section (0 moved in, 0 moved out).'</p>
</blockquote>

<p>The proof I have something fishy is going on is that if I print the count of items on the data set I see exactly 213. So, the dataset matches the correct number and the message is nonsense.</p>

<p>I have had this problem before, <a href="https://stackoverflow.com/questions/36334228/how-to-solve-this-collectionview-crash">here</a> but that was an iOS 7 project. Somehow the problem returned now on iOS 8 and the solutions there are not working and now the dataset <strong>IS IN SYNC</strong>.</p>