This is my code:
- (void) printCount {
NSLog(@"Current second: %i", i++);
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIBackgroundTaskIdentifier taskId = [application beginBackgroundTaskWithExpirationHandler:^{NSLog(@"Task expired");}];
[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(printCount) userInfo:nil repeats:YES];
if (i == 10000) {
[application endBackgroundTask:taskId];
}
}
And this is log that I get:
....
Current second: 579
Current second: 580
Current second: 581
Current second: 582
Current second: 583
Task expired
Current second: 584
Current second: 585
Current second: 586
Current second: 587
Current second: 588
Current second: 589
Current second: 590
....
As I know using beginBackgroundTaskWithExpirationHandler allowed only 10 minutes. I mean this UIBackgroundTaskIdentifier has to “expired” after 10 minutes. And this is really happened (“Task expired” in log)! But why NSTimer continue run? o_O
Regards!
From the documentation for
beginBackgroundTaskWithExpirationHandler:I.e. this is just a notification that the task will expire, it is still your responsibility to close the task (and if you don’t then they system will just kill it after a while).