I'm having a problem trying to change my data source after a PHChange
event occurs. I'm using an NSMutableOrderedSet
as my data source type. When a PHChange
happens, it can give you back three possible indexes: removedIndexes
, insertedIndexes
, and changedIndexes
.
The problem is I'm not sure how I can go about reflecting these changes in the prior data source. Could someone shed some light on how I could convert the indexes given in the PHChange
to update my recentsCollectionDataSource
. My UICollectionView
uses recentsCollectionDataSource
as its data source.
Right now I'm thinking the only way is to manually enumerate and build my own changed indexes instead of using the PHFetchResultChangeDetails
ones.
I'd appreciate any help offered. Any recommendations for alternative ways of making my data source are also welcome.
Code:
@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