iOS:使用“next”分页获取Facebook好友

fd3cxomn  于 2023-06-07  发布在  iOS
关注(0)|答案(2)|浏览(484)

我试图从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,而不是datapaging
只要响应对象中存在“next”值,请帮助正确地逐页获取可标记的朋友。谢谢你。

62lalag4

62lalag41#

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

nbnkbykc

nbnkbykc2#

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

相关问题