iOS Background Mode: After running location updates in the background, the app never terminates

Viewed 239

I'm running location updates in the background. All works well:

  • On significant location the app is launched in the background

- appDidFinishLaunching(options:) is called as expected.

  • I start LocationManager startUpdatingLocation() and startMonitoringSignificantLocationChanges()

  • Locations are collected correctly.

  • After some time, I call stopUpdatingLocation()

  • My program at this stage doesn't need to do anything and no further code is executed

At this point the program stays idle. applicationWillTerminate is never called again. Is this expected? I'd hoped the app will shut down again as no location updates are required.

What's the expected behaviour? Should the app shut down or should it stay idle forever?

Once this happens, then if a user user opens the app or a significant location is received again, then appDidFinishLaunching(options:) is NOT called, but instead applicationDidBecomeActive.

Is there any documentation I can follow that supports the expected behaviour?

2 Answers

Yes, everything here is expected. There's a deleted answer from J.D. Wooder that correctly linked the documentation: "Managing Your App's Life Cycle." As a rule, background iOS apps are not proactively killed. They are only killed when system resources are needed. This is unpredictable, and the app typically will not receive a applicationWillTerminate message when it happens (because the app typically is not running at that point, and it won't be woken up just to kill it). Your app should handled both a cold launch (appDidFinishLaunching) and a warm launch (applicationDidBecomeActive).

Restarting an app from scratch is expensive, so iOS prefers to keep recently used thing in memory if there's no resource pressure. Apps that are doing nothing are very cheap. to keep around.

Note that iOS 13 has grown much more aggressive in killing apps in the background, and that the large cameras on new phones are leading to memory pressures that kill apps more quickly as well, so don't get too comfortable with running in the background for a long time. But it's also very normal.

Please see the iOS app Life Cycle,

appDidFinishLaunching will call only when app Launch,
applicationWillTerminate  - called when app terminate from background
applicationDidBecomeActive - Called when app comes to foreground from background
Related