Why does logging backgroundTimeRemaining show a wrong/big number even though app was moved to background?

≯℡__Kan透↙ 提交于 2019-12-08 06:40:17

问题


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)
        }
    }


}

回答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



来源:https://stackoverflow.com/questions/45314183/why-does-logging-backgroundtimeremaining-show-a-wrong-big-number-even-though-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!