Error UICollectionViewBatchUpdates

I use UICollectionView to display gallery images to customize the selection of photos. In any case, the problem is that when the user clicks on an image that is possible on iCloud and not on the device, the user clicks the Finish button.

Application crash in line, [collectionView performBatchUpdates:]with error:

"NSInternalInconsistencyException". I know what this error means, but cannot solve it.

The code:

dispatch_async(dispatch_get_main_queue(), ^{

    PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.fetchResult];
    if (collectionChanges) {

        self.fetchResult = [collectionChanges fetchResultAfterChanges];

        UICollectionView *collectionView = self.collectionView;
        NSArray *removedPaths;
        NSArray *insertedPaths;
        NSArray *changedPaths;

        if ([collectionChanges hasIncrementalChanges]) {
            NSIndexSet *removedIndexes = [collectionChanges removedIndexes];
            removedPaths = [self indexPathsFromIndexSet:removedIndexes withSection:0];

            NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes];
            insertedPaths = [self indexPathsFromIndexSet:insertedIndexes withSection:0];

            NSIndexSet *changedIndexes = [collectionChanges changedIndexes];
            changedPaths = [self indexPathsFromIndexSet:changedIndexes withSection:0];

            BOOL shouldReload = NO;

            if (changedPaths != nil && removedPaths != nil) {
                for (NSIndexPath *changedPath in changedPaths) {
                    if ([removedPaths containsObject:changedPath]) {
                        shouldReload = YES;
                        break;
                    }
                }
            }

            if (removedPaths.lastObject && ((NSIndexPath *)removedPaths.lastObject).item >= self.fetchResult.count) {
                shouldReload = YES;
            }

            if (shouldReload) {
                [collectionView reloadData];

            } else {

                [collectionView performBatchUpdates:^{
                    if (changedPaths) {
                        [collectionView reloadItemsAtIndexPaths:changedPaths];

                    }

                    if (removedPaths) {
                        [collectionView deleteItemsAtIndexPaths:removedPaths];
                    }

                    if (insertedPaths) {
                        [collectionView insertItemsAtIndexPaths:insertedPaths];
                    }

                    if ([collectionChanges hasMoves]) {
                        [collectionChanges enumerateMovesWithBlock:^(NSUInteger fromIndex, NSUInteger toIndex) {
                            NSIndexPath *fromIndexPath = [NSIndexPath indexPathForItem:fromIndex inSection:0];
                            NSIndexPath *toIndexPath = [NSIndexPath indexPathForItem:toIndex inSection:0];
                            [collectionView moveItemAtIndexPath:fromIndexPath toIndexPath:toIndexPath];
                        }];
                    }

                } completion:^(BOOL finished) {

                }];
            }

            [self resetCachedAssets];
        } else {
            [collectionView reloadData];

        }
    }
    else {

    }
});
+4
source share

Source: https://habr.com/ru/post/1658003/


All Articles