【问题标题】:iOS: fetch Facebook friends with pagination using 'next'iOS:使用“下一个”通过分页获取 Facebook 好友
【发布时间】:2015-04-28 03:02:42
【问题描述】:

我正在尝试从 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);
                                      }];
                                  }
                              }
                          }];
}

问题: 我在“结果”对象中获得前 1000 条记录,并且“下一个”键的值作为递归调用的“参数”参数传递。但是,第二次迭代不会分页并继续返回相同的 1000 条记录。

我还尝试将nextCursor 值用作第二次调用的startWithGraphPath 参数。这导致了一个不同的响应对象,其中的键如 og_objectshareid 而不是 datapaging

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

【问题讨论】:

  • iOS SDK 的 facebook API 文档是否说明了如何处理分页?这是 SDK 3.x 还是 4.x?

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


【解决方案1】:

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

【讨论】:

  • 尝试得到一个响应对象,其结果是键为 'og_object/share/id' 而不是 'data/paging'。
  • if ([[mDicResult valueForKey:@"paging"] objectForKey:@"next"] != nil) { NSString *nextCursor = mDicResult[@"paging"][@"cursors"][@"after"]; [self getsFbTaggableFriends:nextCursor dicFBFriends:dicFriends failure:^(NSError *error) { failureHandler(error); }]; }
  • 很可能带有og_object 结果的那个格式不正确。请注意,作为graph path 参数传递时,它应该只包含路径,即不包含graph.facebook.com
  • 这部分应该是if (nextCursor == nil) { parameters = nil; } else { parameters = [[NSMutableDictionary alloc] init]; [parameters setValue:nextCursor forKey:@"after"]; }
  • 感谢@Anggra 的解决方案。我在这两个地方都进行了更正,现在它可以工作了。
【解决方案2】:

我已经找到了所有答案。大多数答案都建议基于 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 2011-09-24
    相关资源
    最近更新 更多