呼叫完成处理程序方法用于循环

时间:2014-05-25 08:47:24

标签: ios facebook-graph-api recursion facebook-ios-sdk

我在一个阵列中拥有FB用户的albumIds,我想为每个专辑获取图像,我必须使用albumId调用以下方法, 但是我不能在For循环中调用这个方法,我想要所有的专辑图片,问题是For循环执行而不调用“FBRequestConnection startWithGraphPath:graphPath”函数所以我没有得到响应 问题是完成处理程序请告诉我如何在For循环中使用完成处理程序调用方法

 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);
                              }];
}

1 个答案:

答案 0 :(得分:0)

从您的评论来看,Facebook API看起来并不像并发操作。

此代码消除了for循环并在完成处理程序中调用下一个Facebook调用

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];

}