我在应用程序运行时定期调用多个异步RESTful服务,以使某些数据保持最新。此外,我希望能够在收到针对此的推送通知时执行此类数据更新,因此我启用了远程通知的后台模式并实施了didReceiveRemoteNotification:fetchCompletionHandler:
委托方法。
现在我的问题是:由于我的数据更新对多个Web服务执行异步调用,并且根据文档必须调用completionHandler
方法的didReceiveRemoteNotification:fetchCompletionHandler:
,我该如何处理?或者我是否要实现数据更新的同步版本?
由于
答案 0 :(得分:0)
无需对数据更新Web服务进行同步调用。只要您的任务完成,只需致电completionHandler
即可。基本上,您只需30秒即可完成更新工作。成功更新后,只需致电completionHandler
请看以下例如。
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
// Here write logic as per your requirements, check userInfo for notification data
// Following is http call for updation
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"YOUR URL"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:30.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"RESPONSE From Server: %@", response);
// Do any updation work
// Finally call comletionHandler
completionHandler(UIBackgroundFetchResultNoData);
}];
[postDataTask resume];
}