How to solve this CollectionView crash?
<p>My app has two <code>CollectionViewController</code>s. Only one is visible at a given time.</p>

<p>I have created the following structure on storyboard: two container views on top of each other. Every container view has a <code>CollectionViewController</code> embedded. The visibility of a particular container view determines which collectionViewController is visible.</p>

<p>This is the problem. Both <code>CollectionViewController</code>s are receiving data in parallel but iOS has a bug that will make the app crash if one CollectionViewController tries to execute an insert using <code>performBatchUpdates</code> while it is invisible.</p>

<p>Trying to prevent that, I have created a BOOL flag on both <code>CollectionViewController</code>s so they can know if they are visible and execute or not the <code>performBatchUpdates</code>. Something like:</p>

<pre><code>if (self.isThisCollectionViewVisible == NO) return;

[self.collectionView performBatchUpdates:^{
   // bla bla... perform insert,m remove...
</code></pre>

<p>This solves part of the problem. But the app continues to crash on the following condition: if I tap the button to switch to the invisible <code>CollectionViewController</code> making it visible while it is receiving updates.</p>

<p>I mean this: lets call <code>A</code> the first <code>CollectionViewController</code> and <code>B</code> the second one. <code>A</code> is visible and <code>B</code> is invisible at this point. <code>B</code> starts receiving data and is trying to do a <code>performBatchUpdates</code> but as it is invisible, the <code>if (self.isThisCollectionViewVisible == NO) return;</code> is preventing <code>performBatchUpdates</code> to run, what is fine. Now I make <code>A</code> invisible and <code>B</code> visible. At this point the flag <code>self.isThisCollectionViewVisible</code> is set to <code>YES</code> and <code>performBatchUpdates</code> makes the app crash with this error:</p>

<blockquote>
  <p><strong>* Assertion failure in -[CollectionView _endItemAnimationsWithInvalidationContext:tentativelyForReordering:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3512.60.7/UICollectionView.m:4625
  *</strong> 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 (76) must be equal to the number of
  items contained in that section before the update (70), plus or minus
  the number of items inserted or deleted from that section (5 inserted,
  2 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>I think the <code>CollectionViewController</code> is really not yet ready and updated to be able to do a <code>performBatchUpdates</code>... and this is not a matter of not updating the data source previously because it is being updated.</p>

<p>What checks can I do to prevent that from happening?</p>

<p>NOTE: I noticed something strange about this crash in particular. It says that 5 elements are being inserted and 2 deleted but in fact 3 elements are being inserted, 0 deleted and 2 changed when the crashes happen.</p>