UICollectionView performBatchUpdates崩溃

时间:2016-10-17 14:09:52

标签: ios objective-c uicollectionview

我使用UICollectionView显示图库图像以自定义照片选择。无论如何,问题在于,当用户点击可能在 iCloud 上而不在设备上的图像时,用户点击完成。

应用程序崩溃,[collectionView performBatchUpdates:],但错误:

  

" NSInternalInconsistencyException&#34 ;.我知道这个错误意味着什么,但无法解决它。

代码:

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 {

    }
});

0 个答案:

没有答案
相关问题