在我的 iPad 应用程序中,我使用了一个名为 CloudViewController 的 ViewController 和一个名为 SyncServicesController(单例)的“同步引擎”类。2 与 SyncServicesController 中定义的委托方法进行通信。
我所有的同步都正常工作,并传达了 CloudViewController 发生的情况(登录、拉取、推送、错误......等)。
当用户离开应用程序 (applicationWillResignActive) 时会出现问题。
在 AppDelegate 中,我的代码是:
- (void)applicationWillResignActive:(UIApplication*)application
{
UIBackgroundTaskIdentifier backgroundIdentifier = [application beginBackgroundTaskWithExpirationHandler:^(void) {
[application endBackgroundTask:backgroundIdentifier];
[[SyncServicesController sharedServices] cancelAllRequests];
}];
}
并在 SyncServicesController 中取消所有请求:
- (void)cancelAllRequests
{
NSLog(@"CancelAllRequests");
[[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"webapp/services/pull"];
[[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"webapp/services/pull_items"];
[[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"GET" path:@"webapp/services/pull_image"];
[[HTTPClient sharedClient] cancelAllHTTPOperationsWithMethod:@"POST" path:@"webapp/services/push_item"];
[[[HTTPClient sharedClient] operationQueue] cancelAllOperations];
}
当用户离开应用程序时,同步继续执行在 SyncServicesController 中调用的 AFNetworking 块。好的。但是当用户回到应用程序时,它会崩溃。
例如,在 SyncServicesController 中,委托被称为 (EXEC_BAD_ACCESS) :
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
{
[self pullUpdateClientItemsWithServerItems:[JSON valueForKeyPath:@"items"]];
[delegate syncServicesController:self notifyJob:_T(UPDATING_CLIENT_DATA)];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON)
{
}];
就像在后台进入时 CloudViewController 被杀死了......当调用 applicationWillResignActive 时管理 SyncServicesController 委托的最佳方法是什么?