iOS - backgroundTimeRemaining value not showing what expected

倾然丶 夕夏残阳落幕 提交于 2019-12-10 13:28:04

问题


I'm trying to check the value of backgroundTimeRemaining, but I get a very large number. The value of the property is supposed to be the corresponding to 10 min aprox, and I`ve read in Apple documentation here that such value is large when the app is in the foreground. However, I get a large value even when in background:

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

What could am I missing?


回答1:


The watchdog timer is disabled when running from Xcode.

I mostly get sensible values (ie. 600) when running unconnected, but I still get the occasional weird value.



来源:https://stackoverflow.com/questions/15174165/ios-backgroundtimeremaining-value-not-showing-what-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!