iPhone willResignActive 方法未完成执行

标签 iphone objective-c objective-c-blocks

我的应用程序委托(delegate)在进入后台或终止时需要解锁/锁定我的 Microsoft SQL 数据库上的采购订单。

这是我的代码

    - (void)applicationWillResignActive:(UIApplication *)application {

    NSLog(@"App will enter background");
    if ([purchaseOrderIsLocked boolValue] && [purchaseOrderAutoID intValue] > 0) {
        NSLog(@"Unlock Purchase Order because app is in background %i", [purchaseOrderAutoID intValue]);
        [self unlockPurchaseOrderWithAutoID:self.purchaseOrderAutoID];
    }   

- (void)applicationWillEnterForeground:(UIApplication *)application {

   NSLog(@"App will enter foreground");

    if ([purchaseOrderIsLocked boolValue] == FALSE && [purchaseOrderAutoID intValue] > 0) {
        NSLog(@"Lock Purchase Order because app is coming back %i", [purchaseOrderAutoID intValue]);
        [self lockPurchaseOrderWithAutoID:self.purchaseOrderAutoID];
    }
}

-(void)lockPurchaseOrderWithAutoID:(NSNumber *)autoID {

NSLog(@"lock purchase order called with autoID=%i",[autoID intValue]);

    self.client = [SqlClientConnect connect];
    [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '9999' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] 
          withCompletionBlock:^(SqlClientQuery *query) {

              if (query.succeeded) {
                  NSLog(@"locked purchase order");
                  purchaseOrderIsLocked = [NSNumber numberWithBool:YES];
              } else {
                  [self queryFailedWithError:query.errorText]; 
              } 
          }];
}

-(void)unlockPurchaseOrderWithAutoID:(NSNumber *)autoID {
    NSLog(@"unlock purchase order called with autoID=%i",[autoID intValue]);

    self.client = [SqlClientConnect connect];
    [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '0' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] 
          withCompletionBlock:^(SqlClientQuery *query) {

              if (query.succeeded) {
                  NSLog(@"unlocked purchase order");
                  purchaseOrderIsLocked = [NSNumber numberWithBool:FALSE];
              } else {
                  [self queryFailedWithError:query.errorText]; 
              } 
          }];
   }

}

好吧,它对我来说看起来不错......但是当我运行应用程序时,这是我按下主页按钮时从日志中得到的内容

    2011-12-14 13:41:20.558 MA Mobile[2267:707] App will enter background
    2011-12-14 13:41:20.558 MA Mobile[2267:707] Unlock Purchase Order
     because app is in background 18 

2011-12-14 13:41:20.559 MA Mobile[2267:707] unlock purchase order called with autoID=18

它应该一路走下去并发布“解锁采购订单”,但事实并非如此 当我恢复应用程序时,这就是我得到的。

2011-12-14 13:41:44.741 MA Mobile[2267:707] App will enter foreground 1 2011-12-14 13:41:44.973 MA Mobile[2267:707] unlocked purchase order

就像恢复后运行 block 代码一样。这和区 block 有关系吗?我应该做一些不同的事情吗?

最佳答案

一旦您绕过该 block 并返回到 -applicationDidResignActive:,您的应用程序就会立即挂起,并且您启动的异步调用实际上也会暂停。您需要使用-beginBackgroundTaskWithExpirationHandler:让应用程序知道您需要在后台花费更多时间。 。您可以使用如下例所示的代码来完成此操作。有关长时间运行的后台任务的更多信息,请参阅 Background Execution and Multitasking在 iOS 应用程序编程指南中。

这个简单的示例展示了如何在代码中使用此功能:

-(void)unlockPurchaseOrderWithAutoID:(NSNumber *)autoID
{
  NSLog(@"unlock purchase order called with autoID=%i",[autoID intValue]);

  // Start long-running background task
  UIBackgroundTaskIdentifier bti = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:NULL];

  self.client = [SqlClientConnect connect];
  [self.client executeQuery:[NSString stringWithFormat:@"UPDATE PURCHASE SET PURCHASELOCK = '0' WHERE PURCHASEORDERNO = '%i'", [autoID intValue]] withCompletionBlock:^(SqlClientQuery *query) {
    if (query.succeeded) {
        NSLog(@"unlocked purchase order");
        purchaseOrderIsLocked = [NSNumber numberWithBool:FALSE];
    } else {
        [self queryFailedWithError:query.errorText]; 
    }
    // After we complete the asynchronous block, we need to let the system know we can suspend now
    if( bti != UIBackgroundTaskInvalid )
      [[UIApplication sharedApplication] endBackgroundTask:bti];
  }];
}

关于iPhone willResignActive 方法未完成执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8510131/

相关文章:

iphone - 为什么不需要在 NSURLConnectionDelegate 协议(protocol)中声明 NSURLConnection 委托(delegate)方法

objective-c - 白色到 RGB 不正确

iphone - UIImageView更改宽度和高度

ios - 从一个全局位置收听 NSNotification

ios - 启动 Watchkit 扩展时出错

ios - 对象在 block 内初始化后变为 nil

ios - 在不知道高度的情况下在单元格内异步下载图像

iphone - 初始化后更改 NSMutableURLRequest 的 url

iphone - 如何限制 UIDatePicker View 中的日期?

objective-c - 有没有办法将方法作为 block 传递?