首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CLGeocoder如何设置超时?

CLGeocoder如何设置超时?
EN

Stack Overflow用户
提问于 2013-10-18 15:07:59
回答 2查看 765关注 0票数 10

我正在尝试用反向的CLLocation结构来定义城市名称...方法请求CLGeocoder对象,但我不知道如何设置超时?如果没有互联网连接请求时间可能需要大约30秒-它太长了…

EN

回答 2

Stack Overflow用户

发布于 2015-12-21 13:57:06

据我所知,没有任何内置的功能来处理这个问题。我已经开始使用下面的解决方案。抱歉,是在Swift,我不会说流利的Objective-C。

这将为您提供2秒的超时时间

代码语言:javascript
复制
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();
    }
}

在超时触发后,将执行您的回调并调用带有的错误路径。

你会得到一个关于细节的错误:

  • Code = 10
  • 本地化描述(英语)=操作无法完成。(kCLErrorDomain错误10.)

希望这能有所帮助。

票数 5
EN

Stack Overflow用户

发布于 2017-05-31 00:02:42

@JTango18在Swift 3中的答案:

代码语言:javascript
复制
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()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19443635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档