iOS - backgroundTimeRemaining值未显示预期值

问题描述 投票:6回答:1

我正在尝试检查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];
  }
}

我能错过什么?

ios time background multitasking
1个回答
3
投票

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

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

© www.soinside.com 2019 - 2024. All rights reserved.