How to Retrieve the Results of “FBSDKGraphRequest” to Use Outside?

左心房为你撑大大i 提交于 2020-01-03 03:15:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!