ioscore-locationenergyios-background-mode

If I have location services turned on then how can my app get killed when it enters background mode?


My app is getting killed when it's in the background.

However my app is constantly kept alive in the background for locationTracking purposes.

I've spoken to users who have experienced this issue and they're saying that their locationTracking is set to Always and they haven't turned it off.

I'm assuming that even though the app is in background (not suspended) still it can get flushed out of memory due to a memory warning.

My question other than memory is there any other reason? I looked into Apple docs and see that it could be due to a +80% CPU for more than 1 minute in the background but that's not happening. I know that crashes can also be a reason for the app to get launched again, but our crashes haven't increased. We're not seeing any crashes in our locationManager class and the only thing we do in the background is track location and make an API call.

The only thing that I've newly added to my locationManager class is the following extra check:

private func checkLocationAccuracy(from location: CLLocation) throws {

    let ageOfLocation = -location.timestamp.timeIntervalSinceNow

    if ageOfLocation >= maximumAcceptedStale {
        throw LocationAccuracyError.stale(secondsOld: ageOfLocation)
    }

    if location.horizontalAccuracy <= 0 {
        throw LocationAccuracyError.invalid
    }

    if location.horizontalAccuracy > MaximumAcceptedHorizontalAccuracy{
        throw LocationAccuracyError.lowAccuracy(metersOff: location.horizontalAccuracy)
    }
}

If errors are thrown then I just log it. Otherwise I just make a network call. This extra check would reduce network calls, so I'm guessing it should more efficient energy wise.


Solution

  • There was a problem in the code written.

    It was only happening when the user was making changes into the Location (permissions) Settings of the app. Apparently that was a path that testing was not done against!