CLGeocoder how to set a timeout?

I am trying to determine the name of the city using the CLLocation structure with the reverse ... method of querying the CLGeocoder object, but I do not know how to set the timeout? If you don’t have time to connect to the Internet, it may take about 30 seconds - it takes too long ...

+8
ios objective-c
source share
2 answers

There are no built-in functions to handle this that I know of. I started using the following solution. I apologize to Swift, I am not fluent in Objective-C.

This will give you a 2 second timeout

 func myLookupFunction(location: CLLocation) { let timer = NSTimer(timeInterval: 2, target: self, selector: "timeout:", userInfo: nil, repeats: false); geocoder.reverseGeocodeLocation(location){ (placemarks, error) in if(error != nil){ //execute your error handling } else { //execute your happy path } } NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSDefaultRunLoopMode) } func timeout(timer: NSTimer) { if(self.geocoder.geocoding) { geocoder.cancelGeocode(); } } 

After the timeout is triggered, a callback is made and the path to the error is called.

You will receive an error message:

  • Code = 10
  • Localized description (in English) = operation could not be completed. (error kCLErrorDomain 10.)

Hope this helps.

+2
source share

@ JTango18 answer in Swift 3:

 func myLookupFunction(location: CLLocation) { let timer = Timer(timeInterval: 2, target: self, selector: #selector(self.timeout), userInfo: nil, repeats: false); geocoder.reverseGeocodeLocation(location){ (placemarks, error) in if(error != nil){ //execute your error handling } else { //execute your happy path } } RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode) } func timeout() { if (geocoder.isGeocoding){ geocoder.cancelGeocode() } } 
0
source share

All Articles