如何检索" FBSDKGraphRequest"的结果在外面使用?

时间:2015-05-18 10:40:36

标签: ios objective-c facebook facebook-graph-api objective-c-blocks

我正在使用Facebook SDK for iOS。我可以用" NSLog"输出结果。但我不知道如何从FBSDKGraphRequest外部检索结果的值,因为它们位于完成块中。我需要这些值用于以后的操作。我试图将它们放在NSArray或NSMutableArray中,但却无法实现。

插图的代码如下:

    __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]);

这里似乎NSMutableArray的结果是空的,所以,我无法将结果的数据放在那里。如何从" FBSDKGraphRequest"内部检索结果?为了以后使用它们(外部)?什么样的容器可以帮助我?

1 个答案:

答案 0 :(得分:0)

您需要使用[FBSDKTypeUtility arrayValue]来获取数据。

这样的事情:

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];
    }
}];

这能为您解决问题吗?

以下是SDK示例中如何使用它的示例: https://github.com/facebook/facebook-ios-sdk/blob/652fb84a949ef358ff05afedfb2a7c408bd5c839/FBSDKShareKit/FBSDKShareKit/Internal/FBSDKGameRequestFrictionlessRecipientCache.m#L87