iOS:使用“下一个”以分页方式获取Facebook好友

时间:2015-04-28 03:02:43

标签: ios facebook facebook-graph-api pagination next

我正在尝试从Facebook获取'taggable_friends'列表,其中可能有超过1000个可标记的朋友,因此Facebook对结果进行分页。这是方法。

-(void)getsFbTaggableFriends:(NSString *)nextCursor dicFBFriends:(NSMutableArray *) dicFriends failure:(void (^) (NSError *error))failureHandler
{
    NSString *qry = @"/me/taggable_friends";
    NSMutableDictionary *parameters;

    if (nextCursor == nil) {
        parameters = nil;
    }
    else {
        parameters = [[NSMutableDictionary alloc] init];
        [parameters setValue:nextCursor forKey:@"next"];
    }


    [FBRequestConnection startWithGraphPath:qry
                                 parameters:parameters
                                 HTTPMethod:@"GET"
                          completionHandler:^(
                                              FBRequestConnection *connection,
                                              id result,
                                              NSError *error
                                              ) {
                              if (error) {
                                  NSLog(@"%@", [error localizedDescription]);

                              }else {
                                  /* handle the result */
                                  NSMutableDictionary *mDicResult = [[NSMutableDictionary alloc]initWithDictionary:result];

                                  for (NSDictionary * fbItem in [mDicResult valueForKey:@"data"])
                                  {
                                      [dicFriends addObject:fbItem];
                                  }
                                  // if 'next' value is found, then call recursively

                                  if ([[mDicResult valueForKey:@"paging"] objectForKey:@"next"] != nil) {

                                      NSString *nextCursor = mDicResult[@"paging"][@"next"];
                                      NSLog(@"next:%@", [nextCursor substringFromIndex:27]);

                                      [self getsFbTaggableFriends:nextCursor dicFBFriends:dicFriends failure:^(NSError *error) {
                                          failureHandler(error);
                                      }];
                                  }
                              }
                          }];
}

问题: 我在'result'对象中得到前1000条记录,'next'键的值作为递归调用的“parameters”参数传递。但是,第二次迭代不会分页&不断返回相同的1000条记录。

我还尝试使用nextCursor值作为第二次调用的startWithGraphPath参数。它产生了一个不同的响应对象,其中包含og_objectshareid而不是data& paging

只要响应对象中存在“下一个”值,请帮助您逐页正确获取可标记好友。谢谢。

2 个答案:

答案 0 :(得分:1)

使用返回的next端点(图形路径部分,包括光标)作为后续请求的新图形路径,而不是将其作为参数。

答案 1 :(得分:0)

我遇到了所有答案。大多数答案建议基于URL的分页或递归调用函数。我们可以从Facebook SDK本身做到这一点。

      var friendsParams = "taggable_friends"

     // Save the after cursor in your data model
    if let nextPageCursor = user?.friendsNextPages?.after {
        friendsParams += ".limit(10)" + ".after(" + nextPageCursor + ")"
    } else {
        self.user?.friends.removeAll()
    }
    let requiredParams = friendsParams + "{id, name, first_name, last_name, picture.width(200).height(200)}"
    let params = ["fields": requiredParams]
    let _ = FBSDKGraphRequest(graphPath: "me", parameters: params).start { connection, response, error in
        if connection?.urlResponse.statusCode == 200 {
            print("\(response)")
           // Update the UI and next page (after) cursor
        } else {
            print("Not able to fetch \(error)")
        }
    }

您还可以找到示例项目here