同步+ AFNetworking + applicationWillResignActive

时间:2012-05-10 14:14:33

标签: delegates sync afnetworking

在我的iPad应用程序中,我使用名为CloudViewController的ViewController和名为SyncServicesController(singleton)的“同步引擎”类。 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委托的最佳方法是什么?

0 个答案:

没有答案