swift - 使用操作获取 CLLocation 数据

标签 swift cllocationmanager operation

import Foundation
import CoreLocation

class LocationOperation: Operation, CLLocationManagerDelegate {

  // MARK: Properties
  private var manager: CLLocationManager?
  private let handler: (CLLocation) -> Void

  // MARK: Initialization
  init(locationHandler: @escaping (CLLocation) -> Void) {
    self.handler = locationHandler
    super.init()
  }

  // MARK: Main
  override func main() {
    DispatchQueue.main.async {
      let manager = CLLocationManager()
      manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
      manager.delegate = self
      manager.startUpdatingLocation()
    }
  }

  override func cancel() {
    DispatchQueue.main.async {
      self.stopLocationUpdates()
      super.cancel()
    }
  }

  private func stopLocationUpdates() {
    manager?.stopUpdatingLocation()
    manager = nil
  }

  // MARK: CLLocationManagerDelegate
  func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last,
      location.horizontalAccuracy <= manager.desiredAccuracy else {
        return
    }
    stopLocationUpdates()
    handler(location)
  }

  func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    stopLocationUpdates()
    print("Failure to find location") // handle this eventually
    self.cancel()
  }
}

main 在 CLLocationManager 有机会获取位置并将其传递给传入的处理程序之前完成执行。第一个尝试的解决方案是覆盖 isExecuting 属性,并在我调用 (_:didUpdateLocations) 中的 handler(location) 后手动将其设置为 true,但这是一个只读属性。这个link提供了一种方法来做到这一点,但我不确定实现情况。 如何在将位置传递给处理程序之前停止操作完成?。谢谢!

最佳答案

两个严重问题:

  1. managermain() 中本地声明,并在 main() 退出后被销毁。

    替换

    private var manager: CLLocationManager?
    

    private let manager = CLLocationManager()
    

    并删除局部变量

    让 manager = CLLocationManager()

    main()中。我什至更喜欢一个lazy 属性。

  2. 您必须使用异步操作,如here所述

关于swift - 使用操作获取 CLLocation 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52282574/

相关文章:

swift - 访问 tableView :cellForRowAtIndexPath

ios - Mapkit中有没有什么函数可以一直跟踪当前可见区域?

ios - 找不到用户当前位置

ios - 使用swift和Mapkit,显示用户当前位置到特定位置之间的路线

swift - 我如何使用 array.filter 根据属性过滤类对象?

ios - 在 Swift 中对字典使用过滤器

ios - 使用 contentOfFile 而不是名为的图像

r - 在数据帧上查找最大值和最小值,忽略 NA

java - 抛出新的 IllegalOperationException Java

c - 尝试在 C 中执行简单的 do_operation 函数