1

I'm logging my UIApplication.shared.backgroundTimeRemaining but the number is huge. It's almost 200 digits.

This is how I'm logging it.

 os_log("Lat: %f | Long:  %f | RemainingTime: %f ", log: log, type: .default, location.coordinate.latitude, location.coordinate.longitude, UIApplication.shared.backgroundTimeRemaining)

I thought there is something wrong with the format of my logging so I also tried placing a breakpoint and printing it but still the number that it logs is the same huge number. I also looked into this question which has a fair explanation, that is if your app is in foreground then the time would be that huge. But I still see this number even if there has been 5 minutes since I've moved the app to background.

A sample number I get for my remainingTime is:

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

Entire code:

import UIKit
import CoreLocation
import os.log
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate{

    lazy var locationManager : CLLocationManager = {
        var manager = CLLocationManager()
        manager.delegate = self

        manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
        manager.distanceFilter = 1
        manager.pausesLocationUpdatesAutomatically = true
        manager.allowsBackgroundLocationUpdates = true
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()
        return manager
    }()

    var lastLocation : CLLocation?

    var mapView : MKMapView?

    let log = OSLog(subsystem: "XYZ.LocationAppSubSystem", category: "dumbo")

    override func viewDidLoad() {
        super.viewDidLoad()

        if locationManager.location != nil{

        }else {

            DispatchQueue.main.async {
                self.locationManager.startUpdatingLocation()
            }

        }
        os_log("view was Loaded", log: log, type: .error)

        mapView = MKMapView(frame: UIScreen.main.bounds)
        mapView?.showsUserLocation = true
        view.addSubview(mapView!)

    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let location = locations.last else {
            return
        }
        lastLocation = location
        //        let date = Date().description(with: Locale.current)

        os_log("Lat: %{public}f | Long:  %{private}f | RemainingTime: %{public}f ", log: log, type: .default, location.coordinate.latitude, location.coordinate.longitude, UIApplication.shared.backgroundTimeRemaining)

    }

    func locationManagerDidPauseLocationUpdates(_ manager: CLLocationManager) {
        os_log("locationManager was paused", log: log)

        let location = lastLocation

        os_log("Lat: %{public}f | Long:  %{private}f | RemainingTime: %{public}f ", log: log, type: .default, (location?.coordinate.latitude)!, (location?.coordinate.longitude)!, UIApplication.shared.backgroundTimeRemaining)


    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        os_log("Region was exited", log: log)
    }

    func createRegion(location: CLLocation) {

        let radius = 3.0

        let region = CLCircularRegion(center: location.coordinate, radius: radius, identifier: "didPauseLocationUpdates")
        region.notifyOnExit = true
        region.notifyOnEntry = false

        locationManager.startMonitoring(for: region)
    }

    func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
        if region?.identifier == "didPauseLocationUpdates"{
            os_log("Main Region was Failed to be created", log: log)
        }else{
            os_log("Other regions were checked ", log: log)
        }
    }


}
mfaani
  • 33,269
  • 19
  • 164
  • 293
  • 2
    It's probably the highest number that can fit in a double. – dan Jul 25 '17 at 22:10
  • dan: Indeed it is: https://stackoverflow.com/a/17761093 – Ssswift Jul 25 '17 at 22:32
  • 1
    I just ran a quick test and I see that number in the foreground and 179.992160 if I print it in `didEnterBackground` – Paulw11 Jul 25 '17 at 22:37
  • @Paulw11 I pasted all my code. It has complete logging so you can easily debug it or see its log in the Mac Console. Can you take a look? – mfaani Jul 25 '17 at 23:53
  • It seems that if you set `allowsBackgroundLocationUpdates = true` then you will always get the maximum value for a double in `backgroundTimeRemaining`. – Paulw11 Jul 26 '17 at 00:17
  • @Paulw11 Then why does [this](https://stackoverflow.com/questions/17484352/iphone-gps-in-background-never-resumes-after-pause) issue exist? I'm assuming that once you're paused your app will become suspended meaning the backgroundRemainingTime has been zeroed. My whole project is analyzing/validating that issue. Or maybe I didn't wait enough at some location to trigger an increase in BackgroundRemainingTime OR is that once you get the `locationManagerDidPauseLocationUpdates` callback you go from that gigantic number to 0? – mfaani Jul 26 '17 at 00:28
  • It seems that if you have background location updates enabled then then the system is reporting that you have unlimited background time remaining, however background time remaining isn't really related to how long you are going to receive location updates in the background. I don't understand exactly what you are trying to prove with the background time remaining. – Paulw11 Jul 26 '17 at 00:38
  • If you are asking why location updates don't automatically un-pause after pausing then that is by design – Paulw11 Jul 26 '17 at 00:46
  • "isn't really related to how long you are going to receive location updates in the background" True! It's related to how long I'd stop. Usually after 17 minutes it stops. I'm just curious to know what happens to the BackgroundTimeRemaining **how does it go from a infinite number to 0?**. It should be either of the 2 I mentioned already: "Or maybe I didn't wait enough at some location to trigger an increase in BackgroundRemainingTime OR is that once you get the locationManagerDidPauseLocationUpdates callback you go from that gigantic number to 0". – mfaani Jul 26 '17 at 00:49
  • @Paulw11 By knowing that I'd know better how to manipulate/understand the background State... – mfaani Jul 26 '17 at 00:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150127/discussion-between-paulw11-and-honey). – Paulw11 Jul 26 '17 at 01:01

1 Answers1

1

It is because even though the app is in foreground or even closed, when a didEnterRegion, didExitRegion (apparently didUpdateLocations as well) event occurs, the app gets 10 seconds. In those 10 seconds, the backgroundTimeRemaining value will show the same as in foreground. However, if you register for a background task, the backgroundTimeRemaining will show the real background time remaining after those 10 seconds have passed.

In iOS, regions associated with your app are tracked at all times, including when the app isn’t running. If a region boundary is crossed while an app isn’t running, that app is relaunched into the background to handle the event. Similarly, if the app is suspended when the event occurs, it’s woken up and given a short amount of time (around 10 seconds) to handle the event. When necessary, an app can request more background execution time using the beginBackgroundTaskWithExpirationHandler: method of the UIApplication class.

https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/LocationAwarenessPG/RegionMonitoring/RegionMonitoring.html

TudorZg
  • 11
  • 2