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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With