gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:35:34 25 4
gpt4 key购买 nike

是否可以通过委托(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com