0

当我的应用程序进入后台时,我想向网络服务发送最终请求。但似乎它不会等到请求完成。有人可以帮我吗?

这就是我所做的

- (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];


}
4

1 回答 1

0

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

于 2013-05-27T08:11:22.073 回答