Search code examples
iosgpscore-locationcllocationmanagercllocation

Is it possible to get out of order didUpdateLocations when using allowDeferredLocationUpdatesUntilTraveled


I have added [CLLocation allowDeferredLocationUpdatesUntilTraveled: timeout] to my app. Most of the time everything works fine. Every now and then I get the current location when I turn on the screen in the middle of the deferred locations. Is it possible that the location manager would send my app a locationManager:didUpdateLocations: for the current location and then call it with an array of deferred locations? About the locations passed to locationManager:didUpdateLocations Apple's docs say

This array always contains at least one object representing the current location. If updates were deferred or if multiple locations arrived before they could be delivered, the array may contain additional entries. The objects in the array are organized in the order in which they occurred. Therefore, the most recent location update is at the end of the array.

I didn't see anything about the order in which didUpdateLocations is called. Is there any guarantee that the delegate method didUpdateLocations is called in the correct order?

Thanks!

Stephen


Solution

  • So I found the answer to this on developer.apple.com (https://devforums.apple.com/thread/251363?tstart=0). Basically, yes this can happen. When the device wakes up it first sends some individual locations for your current location and then sends the deferred locations. This matches the behavior I see. Here is the response from developer.apple.com.

    I was "sanitising" the time stamps so that my subsequent code would not be confused by time going backwards. I thought I had allowed for this in deferred mode by sorting the batch of deferred locations by time stamp on receipt, but actually I wasn't doing enough. What actually seems to happen when the device wakes after a deferred period is that it sends a handful of individual locations first, with the current time stamp, and then sends the large batch of saved GPS fixes. So the lesson is that if you're going to try to sort your location fixes you need to process more than just a single deferred batch. You really need to merge the new locations into your previously-received locations, going back as far as necessary. it's unfortunate that deferred mode is so difficult to debug.