0

I want to have tasks in backgrounds using location update and using "allowDeferredLocationUpdatesUntilTraveled"

But it does now deferred location, it occurred every singe seconds (It is based on accuracy), and it triggered by condition in locationManager:didFinishDeferredUpdatesWithError:

I tested it based on document

I set Location update background mode

override func viewDidLoad() {
    super.viewDidLoad()
locationManager = CLLocationManager()


    if CLLocationManager.authorizationStatus() == .NotDetermined{
        locationManager.requestAlwaysAuthorization()
    }

    locationManager.delegate = self

    locationManager.pausesLocationUpdatesAutomatically = false
    locationManager.activityType = CLActivityType.Fitness

    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone

    locationManager.startUpdatingLocation()
   }



func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
   if UIApplication.sharedApplication().applicationState == .Background && !self.bDeferringUpdates // && CLLocationManager.deferredLocationUpdatesAvailable()
    {

        self.bDeferringUpdates = true;
          self.locationManager.allowDeferredLocationUpdatesUntilTraveled(CLLocationDistanceMax, timeout: 60*60)  


    }



}


    func locationManager(manager: CLLocationManager, didFinishDeferredUpdatesWithError error: NSError?) {

    self.bDeferringUpdates = false


}

Someone said that it did not worked in debugging mode or charing mode, so I tested it in no charging.

I logged events, but it happens in every single seconds and it did not deferred.

logs are belows

2016-06-09 02:51:39 - didUpdateLocations
2016-06-09 02:51:40 - didUpdateLocations
2016-06-09 02:51:41 - didUpdateLocations
2016-06-09 02:51:42 - didUpdateLocations
2016-06-09 02:51:43 - didUpdateLocations
2016-06-09 02:51:44 - didUpdateLocations
2016-06-09 02:51:45 - didUpdateLocations
2016-06-09 02:51:46 - didUpdateLocations
2016-06-09 02:51:47 - didUpdateLocations
2016-06-09 02:51:48 - didUpdateLocations
2016-06-09 02:51:49 - didUpdateLocations
2016-06-09 02:51:49 - didFinishDeferredUpdatesWithError
2016-06-09 02:51:50 - didUpdateLocations
2016-06-09 02:51:50 - allowDeferredLocationUpdatesUntilTraveled
2016-06-09 02:51:51 - didUpdateLocations
2016-06-09 02:51:52 - didUpdateLocations
2016-06-09 02:51:53 - didUpdateLocations
2016-06-09 02:51:54 - didUpdateLocations

I tested from 1:52 AM to 8:03 AM with no charging

1: 52 AM , with 100% battery, 8:03 AM, with 87% battery

It drains 13% battery during 6 hours

Do I miss it?

dobiho
  • 1,025
  • 1
  • 11
  • 22
  • I found this looking for iOS 10 http://stackoverflow.com/questions/39498899/deferredlocationupdatesavailable-returns-no-in-ios-10 – Mc Gz Mar 25 '17 at 02:04

1 Answers1

0

From your log, I can't tell if it is deferring or not. You need to at-least see what is the error.code is in locationManager:didFinishDeferredUpdatesWithError: and also look at how many location points are you getting in the locations array in locationManager:didUpdateLocations

Errors

  • kCLErrorDeferredFailed then it means GPS was unavailable or Location manager couldn't get a location fix at this time.
  • kCLErrorDeferredNotUpdatingLocation if you failed to start location manager updates.
  • kCLErrorDeferredDistanceFiltered if you set a distance filter (which you didn't).
  • kCLErrorDeferredAccuracyTooLow if your accuracy is lower than 10 meters (seems like yours is good enough).
  • kCLErrorDeferredCanceled if some other error happens (your app is in foreground, your phone is charging, you have another app using continuous location).

I encourage you to see (CLLocationManager's reference)

Also, keep in mind that deferring of location only happens when the OS has an opportunity to conserve battery. This starts with the phone going in low-power stand-by mode. Even if there are background network requests, you might not get location deferring because the CPU is active in the background. (Some people have said over different forums that background audio playback also keeps the phone from going into full standby hence might not be deferring locations).

zakishaheen
  • 5,551
  • 1
  • 22
  • 29