Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Facebook SDK batch requests, single completion block

The iOS Batch Request Page only shows how to execute multiple requests simultaneously and handle their outputs separately. However, I want to make 3 Facebook batch requests simultaneously, and then receive the results of all three in one completion block once all three have completed, so I can aggregate and sort them. Is this possible?

All help is greatly appreciated and I always accept an answer!

like image 938
danielmhanover Avatar asked Dec 15 '13 16:12

danielmhanover


1 Answers

This method worked for me. Here's an example of how to do it requesting the user's info, and the user's friends that use your app. This will return to a single block, and you can parse the result and handle everything at once:

NSArray *requests = @[@{@"method":@"GET",
                        @"relative_url":@"me"},
                      @{@"method":@"GET",
                        @"relative_url":@"me/friends"}];
NSError *encodingError;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:requests options:0 error:&encodingError];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSDictionary *params = @{@"batch":jsonString};

[FBRequestConnection startWithGraphPath:@""
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (error) {
                              NSLog([error description], nil);
                          }
                          else {
                              NSLog(@"Return Data: %@", [result description]);
                          }
                      }];
like image 57
Shaun Budhram Avatar answered Oct 17 '22 00:10

Shaun Budhram