title: Possible to use both reloadItemsAtIndexPaths and reloadData if dataSource count may change?
body: <p>I am investigating a crashing bug with a <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/" rel="nofollow noreferrer"><code>UICollectionView</code></a> via Crashlytics that generally takes this form:</p>
  
  <blockquote>
    <p>Fatal Exception: NSInternalInconsistencyException Invalid update:
    invalid number of items in section 0. The number of items contained in
    an existing section after the update (25) must be equal to the number
    of items contained in that section before the update (27), plus or
    minus the number of items inserted or deleted from that section (1
    inserted, 1 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 believe this is caused because I have a collectionView that periodically refreshes itself with data from a server, and that data from the server may have more or fewer items than are contained in the client <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewDataSource_protocol/" rel="nofollow noreferrer"><code>UICollectionViewDataSource</code></a>.</p>
  
  <p>When I get new data from the server, I call <code>reloadData</code> on my collection view. </p>
  
  <p>However, it is possible that, due to user interaction with my collection view before the network download had completed, I had called <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/reloadItemsAtIndexPaths:" rel="nofollow noreferrer"><code>reloadItemsAtIndexPaths</code></a> just before. <code>reloadItemsAtIndexPaths</code> does not appear to finish for at least a few hundred ms and many processor cycles. Hence, this crash, when the dataSource is updated in the middle of a <code>reloadItemsAtIndexPaths</code>.</p>
  
  <p>Is there an 'immediate' form of <code>reloadItemsAtIndexPaths</code>? Or must I always be calling <a href="https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionView_class/#//apple_ref/occ/instm/UICollectionView/reloadData" rel="nofollow noreferrer"><code>reloadData</code></a> given my use case, which does appear to update everything immediately and leave the <code>UICollectionView</code> in a good state at the end.</p>
  
  <hr>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is what I have done, per advice from TwoStraws:</p>
  
  <pre><code>    // Prevent data source from batch updating while we work
      self.dataSource.locked = YES;
  
      [self.collectionView performBatchUpdates:^{
          [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
      } completion:^(BOOL finished) {
          self.dataSource.locked = NO;
      }];
  </code></pre>
  
  <p>Then in my data source class, upon receipt of results from the server, I always call <code>assignResults</code>:</p>
  
  <pre><code>- (void)assignResults:(NSMutableArray *)newResults {
      if (!self.locked) {
          self.results = newResults;
          [self.delegate handleDataSourceUpdated:self];
      } else {
          self.pendingResults = newResults;
      }
  }
  
  - (void)setLocked:(BOOL)locked {
      _locked = locked;
  
      if (!locked &amp;&amp; self.pendingResults) {
          [self assignResults:self.pendingResults];
          self.pendingResults = nil;
      }
  }
  </code></pre>
  
  <p>As you can see, results only get assigned if the data source is not locked; otherwise they are assigned when the data source is unlocked by the <code>UICollectionViewController</code>. Note that all of these methods are happening on the main thread so I do not need to worry about synchronization of my boolean property, <code>locked</code>.</p>