I am trying to define city name by CLLocation
structure with reverse... method request to CLGeocoder
object, but I don't know how to set timeout? If there are no internet connection request time may take about 30 seconds - it is too long...
There isn't any built in functionality to handle this that I'm aware of. I've started using the following solution. Apologies it's in Swift, I don't speak fluent 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 fires, your callback will be executed and the error path with be called.
You'll get an error with the details:
Hope this helps.
@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()
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With