TWright TWright - 1 month ago 5
iOS Question

iOS Facebook Graph API Use "next" or "previous" Url for Pagination Using SDK

I am not quite sure the best approach or proper SDK call to make when wanting to utilize the "next" or "previous" URLs returned by the graph api when results are paginated. I've reviewed the documentation for FBRequest and FBRequestConnection but there weren't any methods or calls that jumped out at me as the obvious solution to my problem. Anyone have one or can make a suggestion that would point me in the right direction?

Answer

So in my searching for an obvious answer I stumbled upon the Facebook iOS SDK source on github.com and found this class: https://github.com/facebook/facebook-ios-sdk/blob/master/src/Network/FBGraphObjectPagingLoader.m.

Within the "- (void)followNextLink" method I found my solution:

 FBRequest *request = [[FBRequest alloc] initWithSession:self.session
                                                  graphPath:nil];

    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:request completionHandler:
     ^(FBRequestConnection *innerConnection, id result, NSError *error) {
         _isResultFromCache = _isResultFromCache || innerConnection.isResultFromCache;
         [innerConnection retain];
         self.connection = nil;
         [self requestCompleted:innerConnection result:result error:error];
         [innerConnection release];
     }];

    // Override the URL using the one passed back in 'next'.
    NSURL *url = [NSURL URLWithString:self.nextLink];
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
    connection.urlRequest = urlRequest;

    self.nextLink = nil;

    self.connection = connection;
    [self.connection startWithCacheIdentity:self.cacheIdentity
                      skipRoundtripIfCached:self.skipRoundtripIfCached];

The above has/had a lot of code I didn't need so I was able to (with the help of this SO OP) condense it down to:

/* make the API call */
FBRequest *request = [[FBRequest alloc] initWithSession:FBSession.activeSession graphPath:nil];
FBRequestConnection *connection = [[FBRequestConnection alloc] init];
[connection addRequest:request completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithDictionary:@{@"friends": [result objectForKey:@"data"], @"paging": [result objectForKey:@"paging"]}];
    NSLog(@"%@", dictionary);
    block(dictionary, error);
}];

// Override the URL using the one passed back in 'next|previous'.
NSURL *url = [NSURL URLWithString:paginationUrl];
NSMutableURLRequest* urlRequest = [NSMutableURLRequest requestWithURL:url];
connection.urlRequest = urlRequest;

[connection start];

In order to assist others who may be needing a more generic approach, I've compiled much of my Facebook API graph calls into a gist found @ https://gist.github.com/tamitutor/c65c262d8343d433cf7f.

Comments