HOW TO EXECUTE ALASSETLIBRARY CODE WITH VIEWDIDLOAD: METHOD IN IOS?

Eric Martell

Total Post:9

Points:63
Posted by  Eric Martell
 540  View(s)
Ratings:
Rate this:

I am new in iOS programming so I am getting some serious problem in our code. I have getting problem in our asset library code, this code is written in the viewDidLoad: method but when I run the application viewDidLoad: method executes but my asset library code is not executing, it will execute after the execution of other method of application. So, the asset urls that I am fetching from the device library I am unable to display those data into our table view.
this is my assetLibrary code it is not executing with the viewDidLoad: method:

ALAssetLibrary *assetLibrary = [[ALAssetLibrary alloc] init];
[_assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop){
 [group enumerateAssetsUsingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop){
  if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]){
   [urls addObject:[result valueForProperty:ALAssetPropertyURLs]];
  }
 });
} failureBlock:^(NSError *error){
 NSLog(@"Failure");
}];

I google it but I don't find any perfect solution.
Please help me.

  1. Tarun Kumar

    Post:214

    Points:1642
    Re: How to execute ALAssetLibrary code with viewDidLoad: method in iOS?

    The problem you are specifying is not unique so don't worry, every iOS developer face that problem.
    To execute ALAssetLibrary code with the viewDidLoad: method you need to implement dispatch_queue_t in your code and dispatch your queue in syncronized manner using dispatch_sync.
    Write your ALAssetLibrary code in this dispatch_queue_t code.
    See the code below:

    dispatch_queue_t gqueue = dispatch_get_global_queue
                              (DISPATCH_QUEUE_PRIORITY_HIGH, 0);
    dispatch_sync(gqueue, ^(void){
      ALAssetLibrary *assetLibrary = [[ALAssetLibrary alloc]init];
      [assetLibrary anumerateGroupsWithTypes:ALAssetsGroupAll
                    usingBlock:^(ALAssetsGroup *group, BOOL *stop){
                    // implement your asset library code here
      } failureBlock:^(NSError *error){
          NSLog(@"Failure");
      }];
    });
    gqueue = nil;

      Modified On Jan-18-2016 11:57:51 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!