2

I am periodically getting a ~9 second background time remaining. I believe it should be close to 180 seconds. However that's not what I am seeing.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    double secondsToStayOpen = app.backgroundTimeRemaining;
   NSLog(@"secondsToStayOpen %f ",secondsToStayOpen);
 }

prints

secondsToStayOpen 179.920931 
secondsToStayOpen 9.959715 
secondsToStayOpen 9.962670 
madmik3
  • 6,975
  • 3
  • 38
  • 60
  • Did you ever find a answer for this ? – Ryan Heitner Aug 31 '14 at 08:42
  • 1
    Periodically I get 180 , 10 and once I believe I saw 30 seconds. It seems to be random to me. – Ryan Heitner Sep 01 '14 at 10:44
  • no. I submitted a bug to apple and it's still open. however if you ignore it and default set the return to 180 if it is less than 180 it seems to keep it open. – madmik3 Sep 01 '14 at 13:56
  • Thanks for the reply , I was surprised to find that this issue isn't highlighted more. I searched the release notes for where they specify 180 seconds but did not find that either if someone could post a link it would be useful – Ryan Heitner Sep 02 '14 at 07:00

1 Answers1

3

For 180s... you must "create" an iOS Background Task. Please, modify your code whit this:

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_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //### background task starts
        NSLog(@"Running in the background\n");
        while(TRUE)
        {
            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");
}

From here: http://hayageek.com/ios-background-task/

TonyMkenu
  • 7,597
  • 3
  • 27
  • 49
  • a background task is created. but it's created later in the process. – madmik3 Jul 03 '14 at 13:58
  • This is the "only" way to "have" 180s – TonyMkenu Jul 03 '14 at 14:17
  • I don't understand. The background task is created. Does it need to be created prior to calling this method? Is that what you are saying? – madmik3 Jul 03 '14 at 16:00
  • This does not work sometime I get 180 sometimes 10 sometimes 30 – Ryan Heitner Aug 31 '14 at 08:37
  • I can confirm what TonyMkenu is saying - if you ask for backgroundTimeRemaining BEFORE setting up the background task, you'll get 10 seconds. 30 seconds is what is normally seen for background fetches. 180 seconds is what you get if you set up a background task and THEN ask for backgroundTimeRemaining as an app is going into the background. I don't know if it was always like this (I don't believe it was?!), but I have existing code breaking on iOS 8 because I was asking for the remaining time before setting up the background task. – Rob Glassey Oct 15 '14 at 18:23
  • I have the same issue even after following TonyMkenu's example. What I did to resolve it is to use dispatch_get_main_queue() instead of dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), when calling dispatch_async, then it returns 179 consistently. – Yunzhou Aug 19 '15 at 04:16