Uneducated Change Observer PHPhotoLibrary

I seem to have a random problem that I have no idea why this is happening. I can't seem to get photoLibraryDidChange:(PHChange *)changeInstance call an observer. I made several empty projects, and they all demonstrate this problem, the change observer will sometimes need to start the initial installation of the application, but it is never called after I make changes to the Photos application. I also had a reset simulator to no avail. I would appreciate any help offered.

code:

 #import <UIKit/UIKit.h> #import <Photos/Photos.h> @interface ViewController : UIViewController <PHPhotoLibraryChangeObserver> @end - (void)viewDidLoad { [super viewDidLoad]; [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized) { [PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0),^ { [self setup]; }); } }]; } - (void)setup { PHFetchOptions *fetchOptions = [[PHFetchOptions alloc]init]; fetchOptions.wantsIncrementalChangeDetails = YES; PHFetchResult *smartAlbumsFetchResult = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAny options:fetchOptions]; for (PHAssetCollection *sub in smartAlbumsFetchResult) { PHFetchResult *fetch = [PHAsset fetchAssetsInAssetCollection:sub options:fetchOptions]; } } - (void)photoLibraryDidChange:(PHChange *)changeInstance { NSLog(@"Not called"); } - (void)dealloc { [PHPhotoLibrary.sharedPhotoLibrary unregisterChangeObserver:self]; } 
+5
source share
1 answer

I think something is wrong with the way you are testing. It works great for me. Here is what I did.

This is all the code for my one controller:

 #import <UIKit/UIKit.h> @import Photos; #import "ViewController.h" @interface ViewController() <PHPhotoLibraryChangeObserver> @end @implementation ViewController : UIViewController - (void)viewDidLoad { [super viewDidLoad]; [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { if (status == PHAuthorizationStatusAuthorized) { [PHPhotoLibrary.sharedPhotoLibrary registerChangeObserver:self]; } }]; } - (void)photoLibraryDidChange:(PHChange *)changeInstance { NSLog(@"Here"); } @end 
  • I am running the application in Simulator. Authorization required. I allow. Behind the simulator where Xcode works, I see β€œHere” in the console - this is expected, because we receive a notification about the change when the library comes to life after authorization. That is how the observer should be observed.

  • Back in the Simulator, I press Shift-Command-H to jump to the springboard. I switch to the Photos app and delete the photo.

  • In the simulator, I hit Shift-Command-H twice to go to the app switcher.

  • In the simulator, I click on the still running test application to return to it. Behind the simulator in Xcode, I see β€œhere” in the console, because while we were away, the photo was deleted. Again, this is how an observer should behave.

+5
source

All Articles