I am using the PHPhotoLibrary
to retrieve several photo collections like this:
_smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
[_smartAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
_userAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAny options:userAlbumsOptions];
[_userAlbumsFetchResult enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL *stop) {
[self analyzeCollectionInternal:collection];
}];
The Method analyzeCollectionInternal
saves the PHAssetCollections
for later use and displays the contents of the collections.
- (void)analyzeCollectionInternal:(PHAssetCollection *)collection {
NSLog(@"Album Title %@", collection.localizedTitle);
if (![_collections containsObject:collection]) {
[_collections addObject:collection];
}
[...]
}
Additionally I registered a change observer to the Photos library like this:
[[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
The class that acts as Observer implements the PHPhotoLibraryChangeObserver
Protocol like this:
- (void)photoLibraryDidChange:(PHChange *)changeInstance {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"PicturesDataSource - photoLibraryDidChange");
NSMutableArray *collectionsToAnalyze = [NSMutableArray new];
NSMutableArray *collectionsToDelete = [NSMutableArray new];
if (_smartAlbumsFetchResult) {
PHFetchResultChangeDetails *smartAlbumChanges = [changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult];
NSLog(@"PictureDataSource - changeDetailsForFetchResult(_smartAlbumsFetchResult): %@", smartAlbumChanges);
if (smartAlbumChanges) {
_smartAlbumsFetchResult = [smartAlbumChanges fetchResultAfterChanges];
[collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.insertedObjects];
[collectionsToAnalyze addObjectsFromArray:smartAlbumChanges.changedObjects];
[collectionsToDelete addObjectsFromArray:smartAlbumChanges.removedObjects];
}
}
if (_userAlbumsFetchResult) {
PHFetchResultChangeDetails *userAlbumChanges = [changeInstance changeDetailsForFetchResult:_userAlbumsFetchResult];
NSLog(@"PictureDataSource - changeDetailsForFetchResult(_userAlbumsFetchResult): %@", userAlbumChanges);
if (userAlbumChanges) {
_userAlbumsFetchResult = [userAlbumChanges fetchResultAfterChanges];
[collectionsToAnalyze addObjectsFromArray:userAlbumChanges.insertedObjects];
[collectionsToAnalyze addObjectsFromArray:userAlbumChanges.changedObjects];
[collectionsToDelete addObjectsFromArray:userAlbumChanges.removedObjects];
}
}
for (PHAssetCollection *collectionToAnalyze in collectionsToAnalyze) {
[self analyzeCollection:collectionToAnalyze];
}
for (PHAssetCollection *collectionToDelete in collectionsToDelete) {
[self deleteCollection:collectionToDelete];
}
});
}
Now if I open the Photos app to Add/Change/Delete photos from the "All Photos"-SmartAlbum, the Change-Observer gets called, but the call of
[changeInstance changeDetailsForFetchResult:_smartAlbumsFetchResult]
only returns nil
, even if the PHChange object contains insertedObjectIDs
and changedObjectIDs
. The same applies when taking a screenshot using the Home- and the Lock-Button of the iDevice.