Home > DeveloperSection > Forums > How to execute ALAssetLibrary code with viewDidLoad: method in iOS?
Eric Martell
Eric Martell

Total Post:9

Posted on    January-15-2016 7:02 AM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 247  View(s)
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){

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

Tarun Kumar

Total Post:210

Posted on    January-18-2016 11:35 PM

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){
gqueue = nil;

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

Don't want to miss updates? Please click the below button!

Follow MindStick