Question

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!

Was it helpful?

Solution

All ALAssets objects are deallocated when the ALAssetsLibrary that obtained them goes out of scope. If you want to do what you're doing, you'll need to keep a strong reference to library, and then deallocate library when you're done with it.

(In your case, they deallocate at the end of viewDidLoad.)

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