问题
I am using Facebook SDK for iOS. I can output the results with "NSLog" but I do not know how to retrieve the values of the results from FBSDKGraphRequest outside since they are located in a completion block. I need these values for the later manipulations. I tried to put them in NSArray or NSMutableArray but could not make it.
The code for the illustration is given below:
__block NSMutableArray *results;
__block NSArray *obj;
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"user_groups"]) {
NSLog(@"user_groups permission is granted!");
FBSDKGraphRequest *fgr = [[[FBSDKGraphRequest alloc]
initWithGraphPath:@"me/groups?fields=id"
parameters: nil
HTTPMethod:@"GET"]
startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
//here I can output the results
NSLog(@"User's groups:%@", [result[@"data"] valueForKey:@"id"]);
//trying to pass the values to NSMutableArray
obj = [result[@"data"] valueForKey:@"id"];
results = [NSMutableArray arrayWithArray:obj];
}
}];
}
...
NSLog(@"The size of the array of the results : %lu", [results count]);
Here it seems that NSMutableArray of the results is empty, so, I could not put the data of the results there. How can I retrieve the results from inside of "FBSDKGraphRequest" in order to use them later (externally)? What kind of container would help me?
回答1:
You need to use [FBSDKTypeUtility arrayValue] to fetch the data.
Something like this:
NSMutableArray *_results;
FBSDKGraphRequest *request =
[[FBSDKGraphRequest alloc]
initWithGraphPath:@"me/groups?fields=id""
parameters:nil];
[request startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
if (!error) {
NSArray *items = [FBSDKTypeUtility arrayValue:result[@"data"]];
_results = [[NSSet alloc] initWithArray:items];
}
}];
Does this solve the issue for you?
Here is an example of how this is used on the SDK Samples: https://github.com/facebook/facebook-ios-sdk/blob/652fb84a949ef358ff05afedfb2a7c408bd5c839/FBSDKShareKit/FBSDKShareKit/Internal/FBSDKGameRequestFrictionlessRecipientCache.m#L87
来源:https://stackoverflow.com/questions/30301034/how-to-retrieve-the-results-of-fbsdkgraphrequest-to-use-outside