0

Every now and again I get this error when reloading a UICollectionview and I don't think that the error is indicative because the collectionview shouldn't be updating before this call

-(void)loadGallery:(void(^)())completion
{
    [self enumerateAssetsWithCompletion:^(BOOL success, NSMutableArray *assets) {
        if (success)
        {
            self.photos = [assets mutableCopy];
            @try {
                [self.collectionView performBatchUpdates:^{
                    [self.collectionView reloadData];
                } completion:^(BOOL finished) {
                    completion();
                }];
            }
            @catch (NSException *exception) {
                DLog(@"DEBUG: failure to batch update.  %@", exception.description);
            }



        }
    }];
}

- (void)enumerateAssetsWithCompletion:(void(^)(BOOL success,NSMutableArray *assets))completionBlock {
    ALAssetsLibrary* al = [[self class] sharedLibrary];
    __block NSMutableArray* mutableAssets = [NSMutableArray new];
    [al enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup* group, BOOL* stop) {
        if (group == nil) {
            //self.groups = [mutableGroups copy];
            if (completionBlock) {
                completionBlock(YES,mutableAssets);
            }
        }
        else {
            [group setAssetsFilter:[ALAssetsFilter allPhotos]];
            [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
                if (result)
                {
                    [mutableAssets addObject:result];
                }

            }];
        }
    } failureBlock:^(NSError* error) {
        ELog(@"Failed to enumerate groups. Error: %@.", error);
        if (completionBlock)
            completionBlock(NO,nil);
    }];
}

Error :

DEBUG: failure to batch update.  
Invalid update: invalid number of items in section 0.  
The number of items contained in an existing section after the update 
(352) must be equal to the number of items contained in that section before the update (0), 
plus or minus the number of items inserted or deleted from that section 
(0 inserted, 0 deleted) 
and plus or minus the number of items moved into or out of that section 
(0 moved in, 0 moved out).
rdurand
  • 7,342
  • 3
  • 39
  • 72
Avba
  • 14,822
  • 20
  • 92
  • 192

1 Answers1

0

In batch updates block you must perform inserts, deletes, reloads or moves of cells.

Just reloading of collection view inside the batch update block is incorrect as you are not inserting anything, and after the batch update new count of collection view items is grater than it was before.

So, you should either just reload your collection view without batch updates or insert 352 items inside the block.

NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, self.photos.count)];

[self.collectionView performBatchUpdates:^{
                [indexSet enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
                        [self.collectionView insertItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:idx inSection:0]]];
                            }];
            } completion:^(BOOL finished) {
                completion();
            }];

But, you will get much pain with this if you reload your photos more than once in your collection view.

Eugene Dudnyk
  • 5,553
  • 1
  • 23
  • 48