这是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/