ios - 在所有请求完成后执行代码(Facebook SDK)

标签 ios objective-c facebook facebook-graph-api

是否可以通过委托(delegate)以外的方式从异步完成 block 返回?最好通过调用 block 来传递值吗?

- (NSMutableArray *)facebookGalleryImages
{
    // Create array which will hold dicts for each image containing both thumbnail and original image URLs
    NSMutableArray *allFBImages = [[NSMutableArray alloc] init];
    // Begin Facebook API Calls
    [FBRequestConnection startWithGraphPath:@"me/albums" completionHandler:^(FBRequestConnection* connection, id result, NSError* error) {
        if (!error) {
            // Get data of all albums
            NSArray *feed =[result objectForKey:@"data"];
            for (NSDictionary *dict in feed) {
                // Get album ID of each album
                NSString *albumID = [dict objectForKey:@"id"];
                // New API call to get the image data for a specific album ID
                [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/photos", albumID] completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                    if (!error){
                        // Get data of all images in album
                        NSArray *imageFeed = [result objectForKey:@"data"];
                        // For each image data object, extract source image URL and thumbnail URL, then add these to a dictionary
                        for (NSDictionary *dict in imageFeed){
                            NSString *sourceURL = [dict objectForKey:@"source"];
                            NSString *thumbnailURL = [dict objectForKey:@"picture"];
                            NSDictionary *imageDict = @{ @"source" : sourceURL, @"thumbnail" : thumbnailURL};
                            //Add the dictionary holding an images URLs to an array which will be passed to our gallery
                            [allFBImages addObject:imageDict];
                        }
                    }
                }];
            }
        }
    }];
}

最佳答案

您的返回代码不起作用,因为这些方法是异步的。您能做的最好的事情就是扩展您的方法以在完成时返回,因此您的方法将如下所示:

- (void) facebookGalleryImages:(void (^)(NSMutableArray *imagesArray, NSError *error))completion
{
     NSMutableArray *allFBImages = [[NSMutableArray alloc] init];
     // Begin Facebook API Calls
    [FBRequestConnection startWithGraphPath:@"me/albums" completionHandler:^(FBRequestConnection* connection, id result, NSError* error) {
         if (!error) {
            // Get data of all albums
            NSArray *feed =[result objectForKey:@"data"];
            for (NSDictionary *dict in feed) {
                // Get album ID of each album
                NSString *albumID = [dict objectForKey:@"id"];
                // New API call to get the image data for a specific album ID
                [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/photos", albumID] completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                    if (!error){
                        // Get data of all images in album
                        NSArray *imageFeed = [result objectForKey:@"data"];
                        // For each image data object, extract source image URL and thumbnail URL, then add these to a dictionary
                        for (NSDictionary *dict in imageFeed){
                            NSString *sourceURL = [dict objectForKey:@"source"];
                            NSString *thumbnailURL = [dict objectForKey:@"picture"];
                            NSDictionary *imageDict = @{ @"source" : sourceURL, @"thumbnail" : thumbnailURL};
                            //Add the dictionary holding an images URLs to an array which will be passed to our gallery
                            [allFBImages addObject:imageDict];
                        }
                    }
                }];
            }
            completion(allFBImages, nil);
        }else {
           completion(nil, error);
        }
    }];
}

最后,您将使用您的方法:

[self facebookGalleryImages:^(NSMutableArray *imagesArray, NSError *error) {
     if ([imagesArray count] > 0 ){
         // do something with the array
     }else {
         // do something with error
     }     
};

关于ios - 在所有请求完成后执行代码(Facebook SDK),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26432600/

相关文章:

ios - PresentShareDialog 返回 nil 的原因是什么?

ios - 在 Swift 中的 UITableView 上插入一个 float 操作按钮

ios - 在谷歌地图上绘制虚线圆圈 : iOS

objective-c - WatchKit 的社交框架

ios - 在 2017 年单击帖子时,Facebook 应用程序链接不会打开应用程序

ios - 从 Facebook JSON 访问好友数据

iphone - 新手 : Errors in an iPhone app

ios - UIView/CALayer : Transform triggers layoutSubviews in superview

objective-c - 检查我的应用程序用户使用的 iOS 版本

iphone - 使用 Facebook 登录的应用程序登录