ios - UICollectionView-尝试删除并重新加载相同的索引路径

标签 ios objective-c photos photokit

这是PHPhotoLibraryObserver

- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
    dispatch_async(dispatch_get_main_queue(), ^{
        PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.assetsFetchResults];
        if (collectionChanges) {
            self.assetsFetchResults = [collectionChanges fetchResultAfterChanges];
            if (![collectionChanges hasIncrementalChanges] || [collectionChanges hasMoves]) {
                [self.collectionView reloadData];
            } else {
                // if we have incremental diffs, tell the collection view to animate insertions and deletions
                [self.collectionView performBatchUpdates:^{
                    NSIndexSet *changedIndexes = [collectionChanges changedIndexes];
                    if ([changedIndexes count]) {
                        [self.collectionView reloadItemsAtIndexPaths:[changedIndexes indexPathsFromIndexesWithSection:0]];
                    }
                    NSIndexSet *removedIndexes = [collectionChanges removedIndexes];
                    if ([removedIndexes count]) {
                        [self.collectionView deleteItemsAtIndexPaths:[removedIndexes indexPathsFromIndexesWithSection:0]];
                    }
                    NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes];
                    if ([insertedIndexes count]) {
                        [self.collectionView insertItemsAtIndexPaths:[insertedIndexes indexPathsFromIndexesWithSection:0]];
                    }
                } completion:nil];
            }
        }
    });
}

在其他应用程序中删除照片后出现错误

这是一个错误:

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“试图删除并重新加载相同的索引路径({length = 2,path = 0-0})”

当我对PHFetchResult进行如下排序时。如上所述崩溃
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
self.assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];

当我将排序选项设置为nil时。顺利
self.assetsFetchResults = [PHAsset fetchAssetsWithOptions:nil];

我不知道怎么了..

最佳答案

在尝试重新加载索引之前,请先过滤掉已删除的索引:

NSIndexSet *changedIndexes = [changeDetails changedIndexes];
NSMutableIndexSet *safeChangedIndexes = [[NSMutableIndexSet alloc] init];

// Filter out indexes that we've deleted
[changedIndexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL *stop)
{
    if (![removedIndexes containsIndex:index])
    {
        [safeChangedIndexes addIndex:index];
    }
}];
[self.collectionView reloadItemsAtIndexPaths:[safeChangedIndexes indexPathsFromIndexesWithSection:0]];

关于ios - UICollectionView-尝试删除并重新加载相同的索引路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33128336/

相关文章:

html - 水平排列照片

ios - 无法更改 UIPopover 的大小?

ios - 空 while 循环在 iPhone 发布版本中挂起

iphone - 通过游戏中心共享保存的游戏数据?

ios - NSString的范围,更改html标签

iphone - 如何以编程方式将光标移动到下一个 UITextField?

facebook - 检索 Facebook 时间轴封面照片

json - 导出的 Google+ 照片,如何将 JSON 数据转换为 EXIF?

ios - 迅速在iOS中检索json数据

iphone - 推送后如何更改所选单元格的背景颜色?