当要对结果进行分页时,我想使用图API所返回的“下一个”或“上一个” URL时,我不确定最好的方法或正确的SDK调用。我已经查看了FBRequest和FBRequestConnection的文档,但是没有任何方法或调用让我跳出来,这显然是解决我的问题的方法。任何人都可以提出一个建议或可以给我正确方向的建议吗?
因此,在寻找明显答案的过程中,我偶然发现了github.com上的Facebook iOS SDK源,并发现了此类:https : //github.com/facebook/facebook-ios-sdk/blob/master/src/Network/FBGraphObjectPagingLoader .m。
在“ - (void)followNextLink
”方法中,我找到了解决方案:
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];
上面有/没有很多我不需要的代码,因此我能够(借助于此SO OP)将其压缩为:
/* 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];
为了帮助别人谁可能需要一个更通用的方法,我编译很多我的Facebook API调用图形为依据发现@ https://gist.github.com/tamitutor/c65c262d8343d433cf7f。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句