iOS - backgroundTimeRemaining值未显示预期值

时间:2013-03-02 11:57:01

标签: ios time background multitasking

我正在尝试检查backgroundTimeRemaining的值,但是我的数字非常大。该属性的值应该是相应的10分钟aprox,我在Apple文档here中读到,当应用程序在前台时,这样的值很大。但是,即使在后台我也获得了很大的价值:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
      [application endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
   }];

   // Background task
   NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;
   NSLog(@"Background time remaining: %f seconds (%d mins)", timeRemaining, (int)(timeRemaining / 60));

   dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
      NSTimeInterval timeRemaining = [UIApplication sharedApplication].backgroundTimeRemaining;
      NSLog(@"Background time remaining: %f seconds (%d mins)", timeRemaining, (int)(timeRemaining / 60));

      // Perform task

      // Finished
      if (bgTask != UIBackgroundTaskInvalid) {
         [application endBackgroundTask:bgTask];
         bgTask = UIBackgroundTaskInvalid;
      }
  });

  // Start location manager
  if ([CLLocationManager locationServicesEnabled]) {          
     [locationManager startUpdatingLocation];
  }
}

我可能缺少什么?

1 个答案:

答案 0 :(得分:3)

从Xcode运行时禁用看门狗定时器。

我在运行未连接时大多数时候得到合理的值(即600),但我仍然偶尔得到奇怪的值。