除了委托之外,是否可以通过某种方式从异步完成块返回?是否最好通过调用块来传递值?
- (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];
}
}
}];
}
}
}];
}
答案 0 :(得分:1)
您的返回代码无效,因为这些方法是异步的。您可以做的最好的方法是将方法扩展为完成时返回,因此您的方法将如下所示:
- (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
}
};