2015-10-14 50 views
2

這是PHPhotoLibraryObserverUICollectionView - 嘗試刪除並重新加載相同的索引路徑

- (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]; 
      } 
     } 
    }); 
} 

我在其他應用程序

此刪除照片後得到的錯誤是一個錯誤:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to delete and reload the same index path ({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]; 

我不知道什麼是錯的..

回答

-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]]; 
相關問題