3

I have just entered into iOS programming and am struggling with many things.

I am trying to implement small piece of code to getting current location and send it to server in the background. When I call beginBackgroundTaskWithExpirationHandler, I found that backgroundTimeRemaining property returns so big number. Look at the log below the code.

if (self.backgroundTask == UIBackgroundTaskInvalid) {
        NSLog(@"***** startBackground work");

        UIApplication *app = [UIApplication sharedApplication];

        self.backgroundTask = [app beginBackgroundTaskWithExpirationHandler:^{
            NSLog(@"Background handler called. Not running background tasks anymore.");
            [app endBackgroundTask:self.backgroundTask];
            self.backgroundTask = UIBackgroundTaskInvalid;
        }];

        DebugLog(@"====>backgroundTimeRemaining:%.1f seconds", app.backgroundTimeRemaining);
        if (timer == nil) {
            timer = [NSTimer scheduledTimerWithTimeInterval:10 * 60
                                                     target:self
                                                   selector: @selector(timerFired:)
                                                   userInfo:nil
                                                    repeats:YES];
        }
    }

Log:

2013-11-29 09:23:14.852 JeeneeLocatorService[1666:70b] <JLSViewController.m:(317)> ====>backgroundTimeRemaining:179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0 seconds

It does not look normal and I have read from iOS developer library site iOS allows about 10 minutes even though I call beginBackgroundTaskWithExpirationHandler to do long time background task.

I am testing with iOS7 simulator in XCode5. Is it true that I can have that much of time for background job. Your answer would be very appreciated.

EDIT: After getting answer, I move the remaining time display code into the timerFired: method.

- (void)timerFired:(NSTimer *)timer {
        DebugLog(@"***** Timer fired *****");

        UIApplication *app = [UIApplication sharedApplication];
        DebugLog(@"====>backgroundTimeRemaining:%.1f seconds", app.backgroundTimeRemaining);
 }

But, it still gives me same time left whenever timerFired: method is called. Does not this work just in simulator?

sunghun
  • 1,424
  • 4
  • 25
  • 49
  • 1
    What is the type of app.backgroundTimeRemaining? I am guessing it is not a `float` which is why conversion (with the `"%.1f"` specifier) would give you a crazy number (looks bigger than the age of the universe at first glance…) – Floris Nov 28 '13 at 22:35
  • 1
    app.backgroundTimeRemaining is an NSTimeInverval which is a double (representing seconds). That should print nicely with `%.1f`. – Martin R Nov 28 '13 at 22:40
  • I use `%lf` for a double… but maybe I am old fashioned. Is it possible that the variable is not properly initialized? – Floris Nov 28 '13 at 22:43
  • 1
    There is no difference between `%lf` and `%f` when *printing*, because float is promoted to double on a varargs list. – Martin R Nov 28 '13 at 23:00

1 Answers1

8

As your new to ios coding, a little tip: Hold ALT and click on backgroundTimeRemaining. It tell's you it returns an NSTimeInterval which is a double, so using %f is not the problem.

If you click the bottom blue link of the popup it'll open the docs and you'll see it says that this figure will be very large if the app isn't actually in the background.

So I'm guessing this is your issue, that your app is in the foreground when the NSLog is printed.

Darren
  • 10,182
  • 20
  • 95
  • 162
  • 2
    And printing `DBL_MAX` with `%.1f` produces exactly the above output. – Martin R Nov 28 '13 at 23:06
  • Thanks, I am going to move the code into different place. – sunghun Nov 28 '13 at 23:10
  • I have now situation when I launch timer in my view controller and later I go into background mode, but timer is still working giving me once time remaining = 178.93 and in next calls there is DBL_MAX when app state is background! LOL! Why? Is it caused by connection with debugger or because of simulator? I can't check, because I have older Xcode version which is incompatible with my iOS version on iPhone. – Ariel Bogdziewicz Feb 01 '19 at 14:17