RequestLocation () in iOS 9 calls the didUpdateLocation delegate several times

So, on iOS9, a new requestLocation() method has requestLocation() . I try to use it to get only one location update, but it hits didUpdateLocations several times.

Shouldn't it be called only once? I set distanceFilter to 1000.0, so it's pretty wide to get back quickly. Any ideas?

even if I call stopUpdatingLocation() inside the delegate method, I still get three delegate calls.

Note: the same behavior occurs when I use StartUpdatingLocation instead, I want only one return, since I want to get the current country of the user, so send this place to reversegeocoder

early

Here is the code:

 func getLocationOneTime(accuracy: Double){ print("Acquiring location one time - accuracy requested:\(accuracy)") locationManager.distanceFilter = accuracy locationManager.desiredAccuracy = accuracy // Start gpsOneTimeTimeout timer gpsTimeoutTimer = NSTimer.scheduledTimerWithTimeInterval(gpsOneTimeTimeout, target: self, selector: #selector(self.reportTimeout), userInfo: nil, repeats: false) locationManager.requestLocation() } 
+5
source share
1 answer

Usually for this problem, I simply use the if statement to prevent the delegate function changing anything after the first call

in this case, if you are just looking for a place that I would do:

 let locationManager = CLLocationManager() var userLocation: CLLocation! func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if userLocation == nil { if let location = locations.first { userLocation = location self.locationManager.stopUpdatingLocation() } } } 

Edit: I looked at the documentation for the apple for this, and you are doing everything right, I doubt that you can prevent this by reasonable means.

+4
source

Source: https://habr.com/ru/post/1246846/


All Articles