UICollectionView: What am I missing here?

The following alternates between appending an item to a collection-view, and reloading the collection-view with 1 item, at an interval of 0.1s.

(assume it has a cell hooked up in the storyboard with reuse ID "cell")


@interface ViewController : UICollectionViewController
{
    int numItems;
}
@end

@implementation ViewController
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self insert];
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return numItems;
}
- (void)insert
{
    numItems++;
    [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:numItems-1 inSection:0]]];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100*NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
        [self reload];
    });
}
- (void)reload
{
    numItems = 1;
    [self.collectionView reloadData];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 100*NSEC_PER_MSEC), dispatch_get_main_queue(), ^{
        [self insert];
    });
}
@end


It crashes with an assertion in the iOS9.3 simulator

request for index path for global index 8070459302346493018 when there are only 2 items in the collection view'


which is an error I'm occasionally seeing in my app.

If I replace reloadData with reloadSections, it works fine. If I increase the delay from 0.1s to 0.2s, it works fine.



It seems like a very basic use of the API. I am sure I'm missing something really really obvious here, but I can't for the life of me see it. If someone could point it out and make me feed dumb, I would surely appreciate it


Edit: rdar://27354858

Edit: Looks fixed in iOS 10 beta 4.

If I had to guess, your updates are happening faster than the UI animations and the collection view isn't in a consistent state when some of the methods are called.

Sounds like you should file a bug report.

Done.


Was looking for a sanity-check really; it seems like this should be causing crashes all over the place, but I couldn't find any related info about it online which made me worry I was missing something obvious.

UICollectionView: What am I missing here?
 
 
Q