2

I am planning to get/add all the photo Library Image URLs into an NSMutableArray on a separate thread without disturbing the execution of program or main thread.

I need to do the process on application background while other operations are going on.

So I wrote the code below on my App Delegate Class "didFinishLaunchingWithOptions" Delegate and also the ViewDidLoad function of my View Controller Class.

However the other features are not working while the ALAsset block is executing.

But the method "crcGeneration" is working without disturbing other features. As one can see in the code below, I called the method "crcGeneration" inside the ALAsset Block, in the same thread, after got all the Photo Library URLs.

Can any one tell me a good way to resolve the issue?

OnViewDidLoad:

//Calling the getAllURLofPhotLibraryImages Function on a seperate Thread.
[NSThread detachNewThreadSelector:@selector(getAllURLofPhotLibraryImages) toTarget:self withObject:nil];


Function for Getting all image URL's of PhotoLibrary.
**-(void)getAllURLofPhotLibraryImages**
{
   urlStoreArray = [[NSMutableArray alloc] init];     //global array


      void (^assetEnumerator)(struct ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop)
      {
           if(result != NULL)
               {

                // Here storing the asset's image URL's in NSMutablearray urlStoreArr
                NSURL *url = [[result defaultRepresentation] url];
                [urlStoreArray addObject:url];                
          }

      };

      void (^assetGroupEnumerator)(struct ALAssetsGroup *, BOOL *) =  ^(ALAssetsGroup *group, BOOL *stop)
      {

           if(group != nil)
           {
                [group enumerateAssetsUsingBlock:assetEnumerator];
           }
           else
           {       

               **[self crcGeneration];**   //Genearting CRC for PhotoLibrary Images using urlStoreArray after got all the Photo Library URL's .
           }

      };
 ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
  [assetslibrary enumerateGroupsWithTypes:ALAssetsGroupAll       usingBlock:assetGroupEnumerator   failureBlock: ^(NSError *error) {        NSLog(@"Failure" );

     }];

}
adrian
  • 4,574
  • 17
  • 68
  • 119

1 Answers1

0

Try getting the asset URL like this:

NSURL *url = [result valueForProperty:ALAssetPropertyAssetURL];

The implementation of -defaultRepresentation is probably dispatching synchronously onto the main queue.

Adlai Holler
  • 830
  • 7
  • 10