Does the background task stop when the device is locked?

I have a timer when the device enters the background, since I want to save the check on a small amount of data in my service. I am using the following code in the applicationDidEnterBackground method in the application delegate

UIApplication *app = [UIApplication sharedApplication]; //create new uiBackgroundTask __block UIBackgroundTaskIdentifier bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; //and create new timer with async call: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //run function methodRunAfterBackground NSString *server = [variableStore sharedGlobalData].server; NSLog(@"%@",server); if([server isEqual:@"_DEV"]){ arrivalsTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:@selector(getArrivals) userInfo:nil repeats:YES]; } else { arrivalsTimer = [NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(getArrivals) userInfo:nil repeats:YES]; } [[NSRunLoop currentRunLoop] addTimer:arrivalsTimer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; }); 

This works absolutely fine until the device closes automatically and then the timer stops ticking. Any suggestions on how to stop this? The default time is 5 minutes, so most devices will be locked long before it is checked even once.

thanks

+6
source share
1 answer

A few observations:

  • As Mr. X points out, beginBackgroundTaskWithExpirationHandler gives you only three minutes (180 seconds) on modern versions of iOS. Thus, an attempt to start the timer in five minutes will not work.

    You can use [[UIApplication sharedApplication] backgroundTimeRemaining] to find out how much time you have left.

  • When the device locks up, background tasks continue. You should not see the completion of the application. If the user manually terminates the application, “double-click the home button and swipe the task switch screen”, this will kill background tasks, but not just lock the device.

  • A few comments on timers:

    • The code adds a timer to the background queue. Which is not necessary at all. Just because the application is in the background, you can still continue to use the main startup loop for timers, etc.

      So, just call scheduledTimerWithTimeInterval from the main thread, and you're done. It makes no sense to use a GCD workflow with a run loop if absolutely necessary (and even then I could create my own thread and add a run loop to it).

      By the way, if you absolutely need to schedule a timer for some queue to send the background to, it’s easier to use a dispatch timer instead. This completely eliminates the requirement of a run loop.

    • By the way, using scheduledTimerWithTimeInterval with addTimer not practical. You call scheduledTimerWithTimeInterval to create a timer and add it to the current run loop. You use timerWithTimeInterval and then call addTimer if you want to add it to another run loop.

+2
source

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


All Articles