1
votes

I am presenting list of items in UICollectionView and when callback function is called, I am updating array of items, and redrawing collection, but application crashes each time with next message:

*** Assertion failure in -[UICollectionView _endItemAnimations], /SourceCache/UIKit/UIKit-3318.16.25/UICollectionView.m:3765

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert item 1 into section 0, but there are only 1 items in section 0 after the update'

code which I am using is:

items = [databaseManager getNewItems];

[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadItemsAtIndexPaths:[self.collectionView indexPathsForVisibleItems]];
    [self.collectionView reloadData];
} completion:^(BOOL finished) {}];
1

1 Answers

2
votes

I fixed this issue by replacing starting code with this one:

int numberOfOldItems = (int)items.count;
items = [databaseManager getNewItems];

[self.collectionView performBatchUpdates:^{
    NSMutableArray *indexpatharray = [[NSMutableArray alloc]init];
    for (int i = 0; i<numberOfOldItems; i++) {
        [indexpatharray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

    [self.collectionView deleteItemsAtIndexPaths:indexpatharray];
    [indexpatharray removeAllObjects];
    for (int i = 0; i<items.count; i++) {
         [indexpatharray addObject:[NSIndexPath indexPathForRow:i inSection:0]];
    }

    [self.collectionView insertItemsAtIndexPaths:indexpatharray];

} completion:nil];

Code is removing all items from collection view first, and than adding new ones.