我在尝试在PHChange
事件发生后更改我的数据源时遇到问题。我正在使用一个NSMutableOrderedSet
作为我的数据源类型。当 aPHChange
发生时,它可以返回三个可能的索引:removedIndexes
、insertedIndexes
和changedIndexes
。
问题是我不确定如何在先前的数据源中反映这些变化。有人可以解释一下我如何转换中给出的索引PHChange
来更新我的recentsCollectionDataSource
. 我UICollectionView
用作recentsCollectionDataSource
它的数据源。
现在我在想唯一的方法是手动枚举和构建我自己更改的索引,而不是使用PHFetchResultChangeDetails
那些。
我将不胜感激提供的任何帮助。也欢迎任何有关制作我的数据源的替代方法的建议。
代码:
@property (nonatomic, strong) PHFetchResult *assetsFetchResult;
@property (nonatomic, strong) NSMutableOrderedSet *recentsCollectionDataSource;
- (void)setup
{
self.recentsCollectionDataSource = [[NSMutableOrderedSet alloc]init];
self.assetsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum | PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:nil];
for (PHAssetCollection *sub in self.assetsFetchResult)
{
PHFetchResult *assetsInCollection = [PHAsset fetchAssetsInAssetCollection:sub options:nil];
for (PHAsset *asset in assetsInCollection)
{
[self.recentsCollectionDataSource addObject:asset];
}
}
if (self.recentsCollectionDataSource.count > 0)
{
NSArray *array = [self.recentsCollectionDataSource sortedArrayUsingDescriptors:@[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]];
self.recentsCollectionDataSource = [[NSMutableOrderedSet alloc]initWithArray:array];
}
}
- (void)photoLibraryDidChange:(PHChange *)changeInstance
{
PHFetchResultChangeDetails *collectionChanges = [changeInstance changeDetailsForFetchResult:self.assetsFetchResult];
if (collectionChanges)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^
{
self.assetsFetchResult = [collectionChanges fetchResultAfterChanges];
if (!collectionChanges.hasIncrementalChanges || collectionChanges.hasMoves)
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.recentsCollectionView reloadData];
});
}
else
{
dispatch_async(dispatch_get_main_queue(),^
{
[self.recentsCollectionView performBatchUpdates:^
{
NSIndexSet *removedIndexes = [collectionChanges removedIndexes];
NSIndexSet *insertedIndexes = [collectionChanges insertedIndexes];
NSIndexSet *changedIndexes = [collectionChanges changedIndexes];
// Need to update our old recentsCollectionDataSource
// somehow, otherwise we get a crash.
if (removedIndexes.count)
{
[self.recentsCollectionView deleteItemsAtIndexPaths:[removedIndexes indexPathsFromIndexesWithSection:0]];
}
if (insertedIndexes.count)
{
[self.recentsCollectionView insertItemsAtIndexPaths:[insertedIndexes indexPathsFromIndexesWithSection:0]];
}
if (changedIndexes.count)
{
[self.recentsCollectionView reloadItemsAtIndexPaths:[changedIndexes indexPathsFromIndexesWithSection:0]];
}
}completion:NULL];
});
}
});
}
}
@implementation NSIndexSet (Convenience)
- (NSArray *)indexPathsFromIndexesWithSection:(NSUInteger)section
{
NSMutableArray *indexPaths = [NSMutableArray arrayWithCapacity:self.count];
[self enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop)
{
[indexPaths addObject:[NSIndexPath indexPathForItem:idx inSection:section]];
}];
return indexPaths;
}
@end