0

I Have albumIds of FB users in one array, I want to get images for each Album for that I have to call following method with albumId, But I cant call this method in For Loop, I want all albums Images, Problem is that For loop executes without calling "FBRequestConnection startWithGraphPath:graphPath" Function so I am not getting Response The Problem Is Completion Handler Please tell me how to call method with completion Handler in For Loop

 arrAlbumIds=(11523252,11523245,11523278)

 for (int i=0; i<arrAlbumIds.count; i++) {



        NSString * graphPath=[NSString stringWithFormat:@"/%@/photos",[arrAlbumIds objectAtIndex:i]];

        [FBRequestConnection startWithGraphPath:graphPath
                                     parameters:nil
                                     HTTPMethod:@"GET"
                              completionHandler:^(
                                                  FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error
                                                  ) {
                                  /* handle the result */

                                  NSLog(@"album images:%@",result);

                                  FBGraphObject * response=[[FBGraphObject alloc]init];
                                  response=result;

                                  NSMutableDictionary * dictImagesData=[[NSMutableDictionary alloc]init];
                                  dictImagesData=response;


                                  NSMutableArray * arrImagesData=[[NSMutableArray alloc]init];
                                  arrImagesData=[dictImagesData valueForKey:@"data"];


                                  FBGraphObject * fbGraphImages=[[FBGraphObject alloc]init];
                                  fbGraphImages=[arrImagesData valueForKey:@"images"];

                                  NSDictionary * dictImages=[[NSDictionary alloc]init];
                                  dictImages=fbGraphImages;

                                  NSMutableArray * arrImages=[[NSMutableArray alloc]init];
                                  arrImages=[arrImagesData valueForKey:@"images"];



                                  //Get Url of Each Image
                                  //self.arrFbAlbumImages=[[NSMutableArray alloc]init];

                                  [arrFacebookAlbumThumbnail addObject:[arrImages valueForKey:@"source"]];

                                  NSLog(@"arrFacebookAlbumThumbnail=%@",arrFacebookAlbumThumbnail);
                              }];
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    It is unclear exactly what you are asking. There is no recursion in this code – Paulw11 May 25 '14 at 09:04
  • FBRequestConnetion startWithGrapPath: This Method is being called in for loop but for loop execution is not waiting for completion handler thats why completion handler method is not called – user3673300 May 25 '14 at 09:07
  • This is just iteration. It is not recursion http://en.wikipedia.org/wiki/Recursion_(computer_science) The method is asynchronous as network operations take time. Your completion handler will be called when the task completes but you can expect `startWithGraphPath` to return immediately – Paulw11 May 25 '14 at 09:09
  • yes but how to call this method in for loop ? can you help me? – user3673300 May 25 '14 at 09:10
  • What isn't working with what you have now? It looks like you could have some issues with multi-threaded access to `arrFacebookAlbumThumbnail` – Paulw11 May 25 '14 at 09:23
  • For loop is executing but completion Handler block is not executed – user3673300 May 25 '14 at 09:27
  • I suggest you simplify - Try with just one element in the array, or remove the loop and just call for the first element of the array - does your completion handler get called then? – Paulw11 May 25 '14 at 09:29
  • Also - see this - http://stackoverflow.com/questions/18137976/facebook-ios-sdk-3-6-startwithgraphpath-completion-block-not-executed you may have a threading issue inside the FB API – Paulw11 May 25 '14 at 09:30
  • It works fine with one object without loop, But want to get data for all albums so I have to call this method for every albumId exist in array So I have only one way to do that is for loop – user3673300 May 25 '14 at 09:35

1 Answers1

0

From your comments, it looks like the Facebook API doesn't like concurrent operations.

This code eliminates the for loop and invokes the next Facebook call in the completion handler

int i=0;

if (arrAlbumIds.count > 1)
{


  NSString * graphPath=[NSString stringWithFormat:@"/%@/photos",[arrAlbumIds objectAtIndex:i]];

  void (^completionBlock)(FBRequestConnection *connection,id result,NSError *error)= ^( FBRequestConnection *connection,
                         id result,
                         NSError *error) {
    NSLog(@"album images:%@",result);

    FBGraphObject * response=[[FBGraphObject alloc]init];
    response=result;

    NSMutableDictionary * dictImagesData=[[NSMutableDictionary alloc]init];
    dictImagesData=response;


    NSMutableArray * arrImagesData=[[NSMutableArray alloc]init];
    arrImagesData=[dictImagesData valueForKey:@"data"];


    FBGraphObject * fbGraphImages=[[FBGraphObject alloc]init];
    fbGraphImages=[arrImagesData valueForKey:@"images"];

     NSDictionary * dictImages=[[NSDictionary alloc]init];
     dictImages=fbGraphImages;

     NSMutableArray * arrImages=[[NSMutableArray alloc]init];
     arrImages=[arrImagesData valueForKey:@"images"];


     //Get Url of Each Image
     //self.arrFbAlbumImages=[[NSMutableArray alloc]init];

    [arrFacebookAlbumThumbnail addObject:[arrImages valueForKey:@"source"]];

    NSLog(@"arrFacebookAlbumThumbnail=%@",arrFacebookAlbumThumbnail);

    if (++i<arrAlbumIds.count) {
       NSString * graphPath=[NSString stringWithFormat:@"/%@/photos",[arrAlbumIds objectAtIndex:i]];
    [FBRequestConnection startWithGraphPath:graphPath
                                     parameters:nil
                                     HTTPMethod:@"GET"
                              completionHandler:completionBlock];
    }
  };

    [FBRequestConnection startWithGraphPath:graphPath
                                     parameters:nil
                                     HTTPMethod:@"GET"
                              completionHandler:completionBlock];

}
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Does Not work for me, still [FBRequestConnection startWithGraphPath:graphPath parameters:nil HTTPMethod:@"GET" is never executed – user3673300 May 25 '14 at 10:33
  • If you put an NSLog in the dispatch block before the call to startWithGraphPath do you get that printed to the log? – Paulw11 May 25 '14 at 10:40
  • NSlog out side the fb completion handler is printed but FB compeletion handler log is not getting printed – user3673300 May 25 '14 at 10:46
  • I realise now that the because `startWithGraphPath` is asynchronous you will still have the same problem. I will revise my answer. – Paulw11 May 25 '14 at 10:51