【问题标题】:CLGeocoder how to set Timeout?CLGeocoder如何设置超时?
【发布时间】:2013-10-26 22:35:46
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    据我所知,没有任何内置功能可以处理此问题。我已经开始使用以下解决方案。抱歉,它是用 Swift 编写的,我不会说一口流利的 Objective-C。

    这会给你 2 秒的超时时间

    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();
        }
    }
    

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

    您将收到详细信息错误:

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

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      @JTango18 在 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()
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2019-06-25
        • 2011-08-09
        • 2011-06-23
        • 2011-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-15
        相关资源
        最近更新 更多