PHPhotoLibrary change observer not called
Asked Answered
E

1

7

I seem to be having a random problem that I have no idea why is happening. I cannot seem to get the photoLibraryDidChange:(PHChange *)changeInstance to be called by the observer. I've made several blank projects and all are demonstrating this issue, the change observer will sometimes be called upon initial app installation, but is never called after when I perform changes in the Photos app. I've also reset the simulator to no avail. I'd 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];
}
Evert answered 9/6, 2015 at 3:38 Comment(7)
What are you talking about. I go to the Photos app and favorite, unfavorite, and delete the stock images.Evert
Do you not follow? There is no code. This is manipulating IN the Photos app, which is supposed to notifiy you of changes.Evert
@matt Yes, I've created a blank single view project with nothing in it to try to figure out why it's not working now. Not being rude, it was working for me the past few days but isn't now.Evert
@matt, that is all of the code. But I will reformatEvert
@matt I shortened the codeEvert
The app is still running, and my change observer used to be called even when the app wasn't in the foreground.Evert
I have backgrounding setup in the original working app so the changes would still be posted. That's got to be my issue here then!Evert
A
13

I think there is something wrong with how you are testing. It works fine for me. Here's what I did.

This is the entire code of my one view 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 run the app in the Simulator. Authorization is requested. I authorize. Behind the Simulator, where Xcode is running, I see "Here" in the console - this is expected, because we get a change notification when the library "comes to life" after authorization. That is exactly how the observer is supposed to behave.

  • Still in the Simulator, I hit Shift-Command-H to go to the springboard. I switch to the Photos app and delete a photo.

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

  • In the Simulator, I click on the still-running test app to return to it. Behind the Simulator in Xcode, I see "Here" in the console, because while we were out, a photo was deleted. Again, that is exactly how the observer is supposed to behave.

Anton answered 9/6, 2015 at 4:14 Comment(2)
it doenst work if you kill the app & restart until you register again.Arcturus
@KarthikSadhasivam Of course. So? This is like saying "My code doesn't run until I run it." Kind of obvious.Anton

© 2022 - 2024 — McMap. All rights reserved.