I register (write to the file) backgroundTimeRemaining value every time my application wakes up from suspended mode, right before I start the UIApplication main task with an expiration handler (for example, inside my method that makes a network request):
if([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground){
[Logger logIntoFileNamed:@"log" withContent:[NSString stringWithFormat:@" state %ld, background time remaining %.2f",(long)[[UIApplication sharedApplication] applicationState],[[UIApplication sharedApplication] backgroundTimeRemaining ]] andPrettyTime:true];
UIApplication* app = [UIApplication sharedApplication];
__block UIBackgroundTaskIdentifier task = [app beginBackgroundTaskWithExpirationHandler:^{
[app endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
[Logger logIntoFileNamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true];
}];
}
According to the documents, the value backgroundTimeRemainingcan be large if the application is in the foreground:
While the application is running in the foreground, the value in this property remains quite large.
But this is not so. My method executes because the state of the application is equal UIApplicationStateBackground. What am I missing here?