Persilos
Persilos

Reputation: 122

didUpdateLocations delegate method is repeated a random number of times

I get latitude and longitude of the user in my method didUpdateLocations. If location is allowed I call a method that takes in parameters latitude, longitude, and calls a webservice, else I display an UIAlertView.

Problem is: iOS calls my locationManager delegate method a random number of times. So my webservice is called several times... How can I fix it please?

When I call the location, verify if is allowed... I make the request in the previous screen:

    // GET LOCATION
    self.initializeWaitingScreen()
    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedAlways){
            self.locationManager.delegate = self
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.startUpdatingLocation()
    } else {
        let loginFailAlert: UIAlertView = UIAlertView(title: "Localisation refusée", message: "Vos fonctionnalités sont restreintes, pour accéder à l'application complète, veuillez activer la localisation", delegate: self, cancelButtonTitle: "OK")
        loginFailAlert.show()
        self.initializeUIComponent()
        self.initializeDataWithWebServiceWithoutLocation()
    }

My locationManager method:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
            var lon = manager.location.coordinate.longitude.description
            var lat = manager.location.coordinate.latitude.description
            self.locationManager.stopUpdatingLocation()
            self.initializeDataWithWebServiceWithLocation(lon, _lat: lat)
            self.initializeUIComponent()
}

self.initializeDataWithWebServiceWithLocation(lon, _lat: lat) take longitude and latitude, and give it to my method who call webservices.

Upvotes: 2

Views: 1948

Answers (1)

Brad Brighton
Brad Brighton

Reputation: 2184

This is expected behavior. As CLLocationManager determines the user's location, updates will be sent (I don't just mean the obvious). See this excerpt from Apple's docs:

Regardless of which location service you use, location data is reported to your app via the location manager’s associated delegate object. Because it can take several seconds to return an initial location, the location manager typically delivers the previously cached location data immediately and then delivers more up-to-date location data as it becomes available. Therefore it is always a good idea to check the timestamp of any location object before taking any actions. If both location services are enabled simultaneously, they deliver events using the same set of delegate methods.

If you require some filtering of events, you'll need to (1) ensure you've set your desiredAccuracy properly to help minimize the number of events then (2) perform any particular app-specific filtering. Be cautious though, since the reason you get multiple updates is that the determined location has changed. If you second-guess the system, you may wind up with inaccurate data.

Finally, evaluate whether you need location changes or significant location changes. If you don't need the high granularity, go with "significant".

Upvotes: 7

Related Questions