【问题标题】:UICollectionView - attempt to delete and reload the same index pathUICollectionView - 尝试删除并重新加载相同的索引路径
【发布时间】:2016-01-12 17:15:21
【问题描述】:

这是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];

当我将排序选项设置为零时。顺利

self.assetsFetchResults = [PHAsset fetchAssetsWithOptions:nil];

我不知道怎么了..

【问题讨论】:

    标签: ios objective-c photos photokit


    【解决方案1】:

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

    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]];
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 2015-10-01
      • 2021-10-30
      • 2013-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多