Question

i've been working with ALAssetsLibraryChangedNotification in iOS 6.x (6.0.1 specifically at the moment), and i'm getting results are counter to what i'd expect to recieve in my userinfo, based on what I understand from the documentation.

Here's my code for event registration:

  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:_library];

to test, i go into my photo library, and delete some items, add some items.

here's my handler.

    - (void)assetsLibraryDidChange:(NSNotification *)note
{
  NSDictionary* info = [note userInfo];
  NSLog(@"assetsLibraryDidChange calling syncPhotoInfoFromAssets, userInfo %@", info);
  //   If the user information dictionary is nil, reload all assets and asset groups.
  if(note.userInfo==nil) {
    [self syncPhotoInfoFromAssets];
    return;
  }

  //   If the user information dictionary an empty dictionary, there is no need to reload assets and asset groups.
  if(note.userInfo.count == 0) {
    return;
  }

 // If the user information dictionary is not empty, reload the effected assets and asset groups. For the keys used, see “Notification Keys.”
  NSSet *updatedAssets = [info objectForKey:ALAssetLibraryUpdatedAssetsKey];
  NSSet *updatedAssetGroup = [info objectForKey:ALAssetLibraryUpdatedAssetGroupsKey];
  NSSet *deletedAssetGroup = [info objectForKey:ALAssetLibraryDeletedAssetGroupsKey];
  NSSet *insertedAssetGroup = [info objectForKey:ALAssetLibraryInsertedAssetGroupsKey];

  NSLog(@"updated assets:%@", updatedAssets);
  NSLog(@"updated asset group:%@", updatedAssetGroup);
  NSLog(@"deleted asset group:%@", deletedAssetGroup);
  NSLog(@"inserted asset group:%@", insertedAssetGroup);
  //further processing here
}

my output:

   ALAssetLibraryUpdatedAssetGroupsKey = "{(\n    assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED\n)}";
    ALAssetLibraryUpdatedAssetsKey = "{(\n    assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG\n)}";
}
2013-01-06 22:50:45.738 Olesi[25468:3613] updated assets:{(
    assets-library://asset/asset.JPG?id=A695208B-3546-4CCA-B539-B1D132A209B3&ext=JPG
)}
2013-01-06 22:50:45.738 Olesi[25468:3613] updated asset group:{(
    assets-library://group/?id=736B6346-6DA2-4E43-8830-9C263B2D29ED
)}
2013-01-06 22:50:45.739 Olesi[25468:3613] deleted asset group:(null)
2013-01-06 22:51:06.658 Olesi[25468:3613] inserted asset group:(null)

After deleting, and inserting an album, i expect to have received data in both the ALAssetLibraryDeletedAssetGroupsKey and ALAssetLibraryInsertedAssetGroupsKey, and nothing in either of ALAssetLibraryUpdatedAsset*Key. Any ideas? I notice that even Apple's sample code that listens to this notification doesn't even make use of the keys, but rather re-enumerates all assets regardless of the specific key (which smells like they don't trust the notification info)

Was it helpful?

Solution

If you don't have a reference to the group being deleted, the OS doesn't let you know.

I get the correct behavior using the following code:

#import "ROBKViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>

@interface ROBKViewController ()

@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) ALAssetsGroup *currentAssetGroup;

- (void) handleAssetChangedNotifiation:(NSNotification *)notification;

@end

@implementation ROBKViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        _assetsLibrary = [ALAssetsLibrary new];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAssetChangedNotifiation:) name:ALAssetsLibraryChangedNotification object:_assetsLibrary];
    }

    return self;
}

- (void) dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ALAssetsLibraryChangedNotification object:nil];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
    } failureBlock:^(NSError *error) {
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Notification handlers

- (void) handleAssetChangedNotifiation:(NSNotification *)notification
{
    NSLog(@"notification: %@", notification);

    if ([notification userInfo]) {
        NSSet *insertedGroupURLs = [[notification userInfo] objectForKey:ALAssetLibraryInsertedAssetGroupsKey];
        NSURL *assetURL = [insertedGroupURLs anyObject];
        if (assetURL) {
            [self.assetsLibrary groupForURL:assetURL resultBlock:^(ALAssetsGroup *group) {
                self.currentAssetGroup = group;
            } failureBlock:^(NSError *error) {

            }];
        }
    }

}

@end

Note that I only get the notification for the group that assigned to self.currentAssetGroup.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top