我已经实现了performFetchWithCompletionHandler:
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{
NSLog(@"1 performFetchWithCompletionHandler");
MSTable *myTable;
myTable = [self.authService.client tableWithName:@"TestTable"];
[myTable readWithCompletion:^(NSArray *items, NSInteger totalCount, NSError *error) {
if (error) {
NSLog(@"Error in readTable %@",error);
} else
{
NSLog(@"Records: %@",items);
}
}];
NSLog(@"2 performFetchWithCompletionHandler");
//Tell the system that you ar done.
completionHandler(UIBackgroundFetchResultNewData);
}
输出为:
2014-05-23 11:11:10.228 iTest [3096:60b] 1 performFetchWithCompletionHandler 2014-05-23 11:11:10.229 iTest [3096:60b] 2 performFetchWithCompletionHandler
Windows Azure调用未执行?
在didReceiveRemoteNotification上,问题是一样的。
有人知道这个问题吗?
答案 0 :(得分:1)
ReadWithCompletion是一个异步调用,这意味着你的代码将继续执行,并且你传入readWithCompletion(你正在检查错误)的块将不会被执行,直到你真正收到来自你的移动服务的响应。这就是为什么你看到log 1语句后面跟着log 2.你应该将你的调用moveHandler(UIBackgroundFetchResultNewData)移到INSIDE你传递给readWithCompletion的块中,这样在你从Mobile获得实际响应后它会被调用服务。
从评论中,我们发现这在前台运行良好,但在应用程序背景时却没有。当app被后台化并调用它时,只要调用completionHandler(...)就会终止执行。因此,即使正在对移动服务进行调用,completionHandler(...)也会将其删除。解决方案是将completionHandler(...)移动到Mobile Services调用的回调中。