Search code examples
iosfacebookfacebook-graph-apipagination

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.


Solution

  • Use the returned next endpoint (Graph path portion, including the cursor) as the new Graph path for the subsequent request, instead putting it as the parameter.