简体   繁体   中英

iOS: fetch Facebook friends with pagination using 'next'

I am trying to fetch 'taggable_friends' list from Facebook, where there may be more than 1000 taggable friends, so Facebook paginates the results. Here is the method.

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

Problem: I get first 1000 records in the 'result' object and the value of the 'next' key is passed as the "parameters" parameter for the recursive call. However, the second iteration doesn't paginate & keeps returning the same 1000 records.

I also tried using the nextCursor value as the startWithGraphPath parameter for the second call instead. It resulted in a different response object with keys like og_object , share , id instead of data & paging .

Please help to properly obtain the taggable friends page by page, as long as 'next' value is present in the response object. Thank you.

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

I have come across all the answers. Most of the answers are suggesting either URL based pagination or recursively calling function. We can do this from Facebook SDK itself.

      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)")
        }
    }

You can also find the example project here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM