Can't add ALAssets to NSOperationQueue from enumeration block

Viewed 148

I'm trying to enumerate through the ALAssetLibrary to retrieve all saved photos. From the enumeration block, I'm trying to to send each ALAsset for asynchronous processing by passing it to an NSInvocationOperation object, then adding that to an NSOperationQueue. However, only the first ALAsset object is properly passed to the processing method. All subsequent assets are just passed as nil.

Here is my code:

ViewDidLoad:

queue = [[NSOperationQueue alloc] init];
queue.maxConcurrentOperationCount = 1;

ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
     [group setAssetsFilter:[ALAssetsFilter allPhotos]];
     [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop)
      {
          if (! result) {
              return;
          }

          NSInvocationOperation* operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(processAsset:) object:result];
          [queue addOperation:operation];

          return;
      }];

 } failureBlock:^(NSError *error) {
     NSLog(@"%@", error);
 }];

And the -processAsset method

- (void)processAsset:(ALAsset *)asset
{
    NSLog(@"Asset: %@", asset);
    // Asset is nil after the first iteration
}

Any help would be really appreciated, thanks!

1 Answers
Related