我正在尝试从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"键的值作为递归调用的"参数"参数传递。但是,第二次迭代不会分页并不断返回相同的 1000 条记录。
我还尝试使用 nextCursor
值作为第二次调用的 startWithGraphPath
参数。它导致了一个不同的响应对象,其键如og_object
、share
、id
而不是data
&paging
。
请帮助逐页正确获取可标记的好友,只要响应对象中存在"next"值。谢谢。
使用返回的next
终结点(图形路径部分,包括光标)作为后续请求的新图形路径,而不是将其作为参数。
我已经找到了所有的答案。大多数答案都建议基于 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)")
}
}
您还可以在此处找到示例项目