Question

In UICollectionView, I am trying to use performBatchUpdates:completion to perform updates to my grid view. My data source array is self.results.

Here's my code:

dispatch_sync(dispatch_get_main_queue(), ^{

            [self.collectionView performBatchUpdates:^{

                int resultsSize = [self.results count];
                [self.results addObjectsFromArray:newData];

                NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
                if (resultsSize == 0) {
                    [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:0 inSection:0]];
                }

                else {
                    for (int i = 0; i < resultsSize; i++) {
                        [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:resultsSize + i inSection:0]];
                    }
                }

                for (id obj in self.results)
                    [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];

            } completion:nil];

Explanation of what I have / what I'm doing:

This code runs fine when the initial insertion into the collection view is done. However, when I add/insert more data into my collection view (by updating self.results and calling this), this gives the following error:

* 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 (8) must be equal to the number of items contained in that section before the update (4), plus or minus the number of items inserted or deleted from that section (32 inserted, 0 deleted) and plus or minus the number of items moved into or out of that section (0 moved in, 0 moved out).'

I understand this means that the data source wasn't updated properly. However, when querying my self.results array, I see the new count of the data. I am doing that on the first line using addObjectsFromArray. I also stored the old results size in resultsSize. I use that variable to add the newly added index paths into arrayWithIndexPaths.

Now when adding/inserting items, I tried the following for-loops:

for (id obj in self.results) which is what I am using now. It works initially but further inserts crashes.

for (UIImage *image in newData) works as well initially but further inserts crash.

From the name of the function, I believe that insertItemsAtIndexPaths will insert all items at those index paths without a loop. However, without a loop, the app crashes when it initially tries to populate the data.

I also tried looping from resultsSize + 1 until the new self.results count (which contains the new data) and that also crashes at the initial update.

Any suggestions about what I'm doing wrong?

Thank you,

Was it helpful?

Solution

I see several things wrong here. First, I'm not sure why you're using dispatch_sync, I don't have much experience with GCD, and I couldn't make it work with that in there (it seemed to hang, and the UI was unresponsive). Perhaps someone else can help with that. Second, in the loop where you add index paths, you're looping over resultsSize, which as I understand it, is the size of the array before the update, which is not what you want -- you want to start the new indexes at resultsSize and loop up to resultsSize + newData.count. Lastly, when you call insertItemsAtIndexPaths, you want to do that once, not in a loop. I tried this, and it worked to update the collection view (I didn't try it from scratch with an empty collection view):

-(void)addNewCells {
    [self.collectionView performBatchUpdates:^{
        int resultsSize = [self.results count];
        [self.results addObjectsFromArray:newData];
        NSMutableArray *arrayWithIndexPaths = [NSMutableArray array];
        for (int i = resultsSize; i < resultsSize + newData.count; i++) {
            [arrayWithIndexPaths addObject:[NSIndexPath indexPathForRow:i inSection:0]];
        }
            [self.collectionView insertItemsAtIndexPaths:arrayWithIndexPaths];
    }
        completion:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top