在applicationDidEnterBackground中向webservice发送一些东西



当我的应用程序进入后台时,我想向webservice发送最后一个请求。但它似乎不会等到请求完成。有人能帮我一下吗?

这就是我所做的

- (void)applicationDidEnterBackground:(UIApplication *)application{
    NSMutableArray *arrFavorites = [[NSMutableArray alloc]init];
    GenkonStageDataModel *model = [[GenkonStageDataModel alloc]init];
    arrFavorites = [model getAllFavorites];
    API *api = [API new];
  //create dictionary values
   NSManagedObjectContext *context = [RKManagedObjectStore defaultStore].persistentStoreManagedObjectContext;
    NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:uuid,@"uuId",strFav,@"pushFavorits",favPush,@"push", nil];
    RKObjectManager *objectManager = [api mapGetData];
    [objectManager getObjectsAtPath:@"/webservice/apnsusers/update" parameters:dict
                            success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                NSError *error = nil;
                                BOOL success = [objectManager.managedObjectStore.mainQueueManagedObjectContext  save:&error];
                                if (!success) RKLogWarning(@"Failed saving managed object context: %@", error);
                                NSLog(@"updated favorites");
                            }
                            failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                 message:[error localizedDescription]
                                 delegate:nil
                                 cancelButtonTitle:@"OK"
                                 otherButtonTitles:nil];
                                  [alert show];
                                NSLog(@"Hit error: %@", error);
                            }];
    NSError *error2 = nil;
    [context save:&error2];

}

应用程序委托协议的applicationWillResignActive:applicationDidEnterBackground:方法不应该用于向服务器发送请求。应该使用这些方法来保存应用程序的当前状态。所以也许你应该保存你想要发送到服务器的数据/参数(共享的首选项或核心数据),并根据你的需要在applicationDidBecomeActive:applicationWillEnterForeground:执行请求。

最新更新