4

I'm running test on measuring how much time left for background task time remaining. I have read several examples and saw that everyone doing it like:

UIApplication *app = [UIApplication sharedApplication];
double *bgTime = app.backgroundTimeRemaining;

NSLog(@"Background Time Remaining: %f", bgTime);  

But I get very long value like 1797693134862315708145274237317043567980705675258449965989...

I understand that [[UIApplication sharedApplication] backgroundTimeRemaining] is NSTimeInterval, but is there any way to convert this to just x seconds?

I have also tried this, but it won't work.

Any help would be appreciated.
Thank you.

Community
  • 1
  • 1
Faust V
  • 667
  • 1
  • 9
  • 19
  • NSTimeInterval IS specified in seconds. Are you sure you're calculating it correctly? – Mike T Dec 17 '12 at 06:41
  • Thanks for reply! I thought NSTimeInterval was specified in seconds... How should I calculate and convert it to seconds to display backgroundTimeRemaining? – Faust V Dec 17 '12 at 07:18

2 Answers2

2

Because NSTimeInterval is a float or double, but not a pointer. double *bgTime should be double bgTime.

2

If the application is in the foreground, the time will be large as per documentation here.

Paramasivan Samuttiram
  • 3,728
  • 1
  • 24
  • 28
  • Thanks again Paramasivan Samuttiram! I'm testing this in background mode by quit the app with home button, but they're still large number and couldn't be converted to seconds... Interestingly, the number looks never changing as if they're in the foreground... Does this mean anything relevant? – Faust V Dec 17 '12 at 07:12
  • Hi. Currently I'm writing this inside a method which called by NSTimer every x seconds. The NSTimer method are called right after the beginBackgroundTaskWithExpirationHandler is called. I thought this [UIApplication sharedApplication].backgroundTimeRemaining can be retrieved from anywhere... – Faust V Dec 17 '12 at 07:30
  • After some research, it seems that the problem is my backgroundTimeRemaining value is foreground and looks like I didn't display it properly when in background... In my simplified test, it showed remaining time correctly with NSLog(@"%0.f"); And I still cannot figured out why my backgroundTimeRemaining was not decreased... Anyway, thanks again for your answer, which led me to the right direction to solve this! – Faust V Dec 20 '12 at 03:59