gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:06:01 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com