Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - backgroundTimeRemaining value not showing what expected

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?

like image 528
AppsDev Avatar asked Mar 02 '13 11:03

AppsDev


1 Answers

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.

like image 95
Gordon Dove Avatar answered Nov 09 '22 22:11

Gordon Dove