我想在应用处于后台时执行图形API调用。
我有一个方法可以执行以下操作(我将仅包含相关部分):
- (void) facebookFetch
{
NSString* query = @"<my_query">;
NSDictionary *queryParam = @{ @"q": query };
[FBSettings setLoggingBehavior:[NSSet setWithObjects:
FBLoggingBehaviorFBRequests,
nil]];
// Make the API request that uses FQL
[FBRequestConnection startWithGraphPath:@"/fql"
parameters:queryParam
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
} else {
NSLog(@"Result: %@", result);
}
}];
}
我在这里的是,如果我在应用程序处于前台时调用此函数,我会收到回复。
现在问题是我需要在后台调用此函数,所以我已经实现了以下功能:
/** Background task **/
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"####### background task!");
if (![FBSession activeSession].isOpen) {
NSLog(@"fbsession is NOT open!");
} else {
NSLog(@"fbsession is open!");
[self facebookFetch];
}
completionHandler(UIBackgroundFetchResultNewData);
}
我得到了#34; fbsession是开放的!&#34;消息,所以我正确输入相关代码,但后来我没有收到任何回复。
正如您所看到的,我记录了请求,以及我在中的 :
URL: https://graph.facebook.com//fql?sdk=ios&access_token=ACCESS_TOKEN_REMOVED&q=<my_query>&migration_bundle=fbsdk%3A20131212&format=json
Method: GET
UserAgent: FBiOSSDK.3.13.1
MIME: multipart/form-data; boundary=3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f
但正如我之前告诉你的那样,在后台情况下,我没有收到回复。
任何帮助?
答案 0 :(得分:0)
您需要在completionHandler(UIBackgroundFetchResultNewData);
的完成模块中调用facebookFetch
。
- (void) facebookFetch:(void (^)(UIBackgroundFetchResult))backgroundCompletion
{
NSString* query = @"<my_query">;
NSDictionary *queryParam = @{ @"q": query };
[FBSettings setLoggingBehavior:[NSSet setWithObjects:
FBLoggingBehaviorFBRequests,
nil]];
// Make the API request that uses FQL
[FBRequestConnection startWithGraphPath:@"/fql"
parameters:queryParam
HTTPMethod:@"GET"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if (error) {
NSLog(@"Error: %@", [error localizedDescription]);
} else {
NSLog(@"Result: %@", result);
}
if (backgroundCompletion) {
backgroundCompletion(UIBackgroundFetchResultNewData);
}
}];
}
/** Background task **/
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
NSLog(@"####### background task!");
if (![FBSession activeSession].isOpen) {
NSLog(@"fbsession is NOT open!");
} else {
NSLog(@"fbsession is open!");
[self facebookFetch:completionHandler];
}
}
您需要更新对facebookFetch:
的任何其他来电,并且您可以传递NULL
(或nil
)。