我在PHChange
事件發生後嘗試更改我的數據源時出現問題。我正在使用NSMutableOrderedSet
作爲我的數據源類型。當PHChange
發生時,它可以給你回三個可能的索引:removedIndexes
,insertedIndexes
和changedIndexes
。Photos API將PHFetchResultChangeDetails索引轉換爲NSMutableOrderedSet
問題是我不知道如何才能在前面的數據源中反映這些更改。是否有人可以告訴我如何轉換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