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

时间:2015-10-14 14:36:45

标签: 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];

我不确定是什么错误..

1 个答案:

答案 0 :(得分:-1)

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

public class Result implements Comparable
    {
        private int count;
        private String value;

        public Result(String value)
        {
            this.value = value;
            this.count = 0;
        }

        public String getValue()
        {
            return value;
        }

        public int getCount()
        {
            return count;
        }

        public void increment()
        {
            count++;
        }

        @Override
        public int compareTo(Object o)
        {
            Result result = (Result)o;
            if(getCount() == result.getCount())
            {
                return getValue().compareTo(result.getValue());
            }
            else
            {
                // Want larger values to be on-top, so reverse traditional sort order
                return ((Integer)result.getCount()).compareTo((Integer)getCount());
            }
        }
    }