Why does backgroundTimeRemaining give DBL_MAX, even if the application state is UIApplicationStateBackground?

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?

+4
2

, :

backgroundTimeRemaining , .

applicationDidEnterBackground backgroundTimeRemaining. 180. dispatch_async Foreground ( > 180).

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    if([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
    {
        NSLog(@"Multitasking Supported");

        __block UIBackgroundTaskIdentifier background_task;
        background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

            //Clean up code. Tell the system that we are done.
            [application endBackgroundTask: background_task];
            background_task = UIBackgroundTaskInvalid;
        }];

        //To make the code block asynchronous
        dispatch_async(dispatch_get_main_queue(), ^{

            //### background task starts
            NSLog(@"Running in the background\n");
            while(TRUE)
            {
                //#### Filter Foreground Values
                if ([[UIApplication sharedApplication] backgroundTimeRemaining] < 180) {
                    NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
                    [NSThread sleepForTimeInterval:1]; //wait for 1 sec
                }

            }

            //#### background task ends
            //Clean up code. Tell the system that we are done.
            [application endBackgroundTask: background_task];
            background_task = UIBackgroundTaskInvalid;
        });
    }
    else
    {
        NSLog(@"Multitasking Not Supported");
    }

}
0

, backgroundTimeRemaining. :

UIApplication *app = [UIApplication sharedApplication];
long appState = (long)app.applicationState;

if (appState == UIApplicationStateBackground) {
   __block UIBackgroundTaskIdentifier task = [app beginBackgroundTaskWithExpirationHandler:^{

     [app endBackgroundTask:task];
     task = UIBackgroundTaskInvalid;
     [Logger logIntoFileNamed:@"log" withContent:@" expiration handler executed " andPrettyTime:true];
  }];

  NSTimeInterval backgroundTimeRemaining = app.backgroundTimeRemaining;
  [Logger logIntoFileNamed:@"log" withContent:[NSString stringWithFormat:@" state %ld, background time remaining %.2f", appState, backgroundTimeRemaining] andPrettyTime:true];
}
0

Source: https://habr.com/ru/post/1661175/


All Articles