Andrey Banshchikov
Andrey Banshchikov

Reputation: 1548

Facebook API Pagination doesn't work

I use Facebook SDK 3.10.0 for iOS. When i'm trying to get user newsfeed, i've a problem: On facebook request:

[FBRequest requestForGraphPath:@"me/home"]

i get response with all data that i need and info about paging:

data = (
    "A LOT OF USER DATA"
);
paging =     {
    next = "https://graph.facebook.com/{MY_USER_ID}/home?format=json&access_token={ACCESS_TOKEN}&limit=25&until=1385113385";
    previous = "https://graph.facebook.com/{MY_USER_ID}/home?format=json&access_token={ACCESS_TOKEN}&limit=25&since=1385206398&__previous=1";
};

but when i trying to get info using next link and AFHTTPRequestOperation:

AFHTTPRequestOperation *newDataOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    newDataOperation.responseSerializer = [AFJSONResponseSerializer serializer];
    [newDataOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Next page retrieving error = %@", error);
    }];

i get empty json

data =     (
);

i've using Facebook Access Token Debugger and result:

Access Token Debugger Result

And there is strange thing access token which i get using Graph Api Explorer differs from which i get in my app. Token which i get from Graph Api Explorer works perfectly fine but when i tesetd token from app results the same: me/home working enter image description here but next link returns empty:

enter image description here

Upvotes: 1

Views: 1822

Answers (2)

Avinash
Avinash

Reputation: 4362

For pagination of facebook I would suggest to use apple native class as

Use nextPageURL variable to cache the next url from JSON response and assign to url string on next api request if nextPageURL is not nil and use following piece of code:

if (self.nextPageURL) {
    // urlString is the first time formulated url string
    urlString = self.nextPageURL;
}
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:networkQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    if (error) {
        DDLogVerbose(@"FACEBOOK:Connection error occured: %@",error.description);
    }else{
        isRequestProcessing = NO;
        NSDictionary *resultData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        DDLogVerbose(@"parsed data is %@",resultData);
        self.nextPageURL = resultData[@"paging"][@"next"];
        // do your customisation of resultData here.
    }
}];

Upvotes: 0

Rahil Arora
Rahil Arora

Reputation: 3674

And there is strange thing access token which i get using Graph Api Explorer
differs from which i get in my app.

It's actually not strange. The access token will change depending on the type of application you've selected from the top right corner of the explorer.

Facebook API Pagination doesn't work

I've seen a lot of questions about the pagination problem in the Graph API and it's strange that the problem hasn't been fixed yet! You can try using the Offset based pagination. For example, setting limit=100&offset=0 in the URL. I know this doesn't make sense here in case of news feed, but it usually works fine in case of large volume of data.

Upvotes: 1

Related Questions