iOS - backgroundTimeRemaining value not showing what expected
Asked Answered
B

1

6

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?

Burden answered 2/3, 2013 at 11:57 Comment(6)
I think the background task won't be started until the applicationDidEnterBackground event has been handled completely.Ita
Then... how could I request the backgroundTimeRemaining value?Burden
try within a dispatch_async block on the main threadIta
your code is working in the iOS simulator, I got 10 minutes background time.Ita
really? I get weird values both in simulator and device...Burden
I'm also getting weird values in both simulator and device (like 2147483648 mins). Did you ever figure this out?Cathrinecathryn
K
4

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.

Krystalkrystalle answered 15/8, 2013 at 15:29 Comment(1)
That's weird since you can't then test against failures. I wish this wasn't the case.Ovary

© 2022 - 2024 — McMap. All rights reserved.