除了委派以外,是否可以从异步完成块返回?最好通过调用一个块来传递值吗?
- (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
}
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句