startMonitoringSignificantLocationChanges not starting instantly

Viewed 221

I'm working on a navigation application, everything working in terminated, background and fore ground state. But in one scenario of terminated state startMonitoringSignificantLocationChanges is not handling itself.

The issue is

  • when i start the startMonitoringSignificantLocationChanges and killed the app, then I'm getting location event like after 0.5-1km because of that it draws straight line from my initial position to the first location event I get.But when the location event starts coming then everything work smoothly
  • Same issue occur again when in the middle of travelling I open the application to check my route status and then kill the application, again location events start coming after 0.5-1km and a straight line was drawn.

The code is straight

    significantLocationManager = CLLocationManager()
    significantLocationManager?.allowsBackgroundLocationUpdates = true
    significantLocationManager?.pausesLocationUpdatesAutomatically = false
    significantLocationManager?.requestAlwaysAuthorization()

and call the tracking when user needs by

significantLocationManager?.startMonitoringSignificantLocationChanges()

Rest I have handled the incoming location event in the app delegate to save in db.

So question is how should I handle this scenario in which straight line is drawn ?

2 Answers

From Apple documentation:

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.

If you need to receive location updates as soon as possible I'd recommend to use startUpdatingLocation() with desired distanceFilter of CLLocationManager.

You can use Location update in background mode. From Apple documentation:

When you start the significant-change location service, a recently cached value may be reported to your delegate immediately. As new location data is obtained, the location manager calls your delegate's locationManager(_:didUpdateLocations:) method with the updated values. The locations parameter always contains at least one location and may contain more than one. Locations are always reported in the order in which they were determined, so the most recent location is always the last item in the array, as shown in Listing 2.

func locationManager(_ manager: CLLocationManager,  didUpdateLocations locations: [CLLocation]) {
   let lastLocation = locations.last!
               
   // Do something with the location. 
}

Here you will get the last cached location in your device, and it should be very precise if you have location service turned on in your device of course.

Another thing to know is this. Note form Apple:

The significant-change location service requires authorization. For more information Requesting Authorization for Location Services.

Related