iosswiftiphone

iOS 12 being more aggressive about closing my app?


I wrote a stat tracker for skiing / snowboarding that was working just fine last season. This season I'm having the hardest time keeping the app alive even though I'm setup to receive background location updates.

Here are some of the tasks that are running in the background on timers:

As long I have cell signal things have a better chance of staying alive but no guarantee.

Did something change in iOS 12 vs 11?

Here's my code for starting location updates:

locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = .other
locationManager.allowsBackgroundLocationUpdates = true
            
locationManager.pausesLocationUpdatesAutomatically = false
locationManager.showsBackgroundLocationIndicator = true
            
locationManager.startUpdatingLocation()

I've tried locationManagerDidPauseLocationUpdates to restart location updates if iOS tried to kill it, changing the activity type, changing the accuracy, nothing seems to work although changing the activity type to other did extend the amount of time before the app dies so I thought locationManagerDidPauseLocationUpdates would restart it but no dice.

Any other ideas on keeping things alive while recording?

I've also tried checking for memory leaks

Screenshot of Info.plist:

enter image description here


Solution

  • So I don't know why but after some experimentation and moving things around I seem to have fixed it.

    I had CllocationManager as a singleton and moved it to a struct as I'm sharing it between multiple VCs as to not instantiate it more than once.

    I then moved

        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.activityType = .other
        locationManager.allowsBackgroundLocationUpdates = true
        locationManager.pausesLocationUpdatesAutomatically = false
        locationManager.showsBackgroundLocationIndicator = true
    

    out of its own function and into viewDidLoad() which still didn't work so I moved it to viewDidAppear() and now my app has been running for 8 hours without a hiccup. I'm not sure if the way I was sharing the location manager before was causing a second instantiation and a reset of the location manager settings to default which would cause locationManager.pausesLocationUpdatesAutomatically to become true (not sure just guessing)