"Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash"



我在控制台中看到此错误,但不确定原因是什么? 我请求用户位置一次。

2018-09-12 20:04:26.912292-0400 Watch Extension[40984:3250895] [Client] Failure to deallocate CLLocationManager on the same runloop as its creation may result in a crash

代码如下:

import CoreLocation

class WorkoutLocationManager: NSObject, CLLocationManagerDelegate {
private var locationManager: CLLocationManager?
public var formattedWorkoutAddress: String?
public func getWorkoutLocation() {
guard CLLocationManager.locationServicesEnabled() else {
print("User does not have location services enabled")
return
}
locationManager = CLLocationManager()
locationManager?.delegate = self
locationManager?.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
let locationAuthorizationStatus = CLLocationManager.authorizationStatus()
switch locationAuthorizationStatus {
case .authorizedAlways:
print("location authorized Always")
locationManager?.requestLocation()
case .authorizedWhenInUse:
print("location authorized When in Use")
locationManager?.requestLocation()
case .denied:
print("location authorization denied")
case .notDetermined:
print("location authorization not determined")
case .restricted:
print("location authorization restricted")
}
}

// MARK: - CLLocationManagerDelegate
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let currentLocation = locations.first else { return }
let geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(currentLocation) { (placemarksArray, error) in
if let unwrappedError = error  {
print("Geocoder error: (unwrappedError)")
}
guard let placemarksArrayUnwrapped = placemarksArray else  { return }
if placemarksArrayUnwrapped.count > 0 {
if let placemark = placemarksArray?.first {
let name = placemark.name ?? ""
let subLocality = placemark.subLocality ?? ""
let locality = placemark.locality ?? ""
let state = placemark.administrativeArea ?? ""
//                            print("address1:", placemark.thoroughfare ?? "")
//                            print("address2:", placemark.subThoroughfare ?? "")
//                            print("city:",     placemark.locality ?? "")
//                            print("state:",    placemark.administrativeArea ?? "")
//                            print("zip code:", placemark.postalCode ?? "")
//                            print("country:",  placemark.country ?? "")
let workoutLocationAsString = (name + " " + subLocality + " " + locality + " " + state)
print("workoutLocationAsString = (workoutLocationAsString)")
self.formattedWorkoutAddress = workoutLocationAsString
} else { print("no placemark")}
} else { print("placemark.count = 0")}
}
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("location manager error = (error)")
}
}

用法:

private func handleWorkoutSessionState(didChangeTo toState: HKWorkoutSessionState,
from fromState: HKWorkoutSessionState) {
switch (fromState, toState) {
case (.notStarted, .running):
workoutLocationManager.getWorkoutLocation() 

必须在运行循环中创建CLLocationManager。如果不这样做,您将收到来自CoreLocation的以下警告:

位置管理器是在主线程以外的线程上执行的调度队列上创建的。 开发人员有责任确保在分配位置管理器对象的线程上运行运行循环。 特别是,不支持在任意调度队列(未附加到主队列(中创建位置管理器,这将导致无法接收回调。

在您的情况下,它看起来像您的locationManager它是在main thread中创建的,但是WorkoutLocationManager的实例正在其他线程上使用,这导致它在此线程中被释放,因此,在同一线程中释放locationManager

一种选择是确保仅在主线程中创建和使用WorkoutLocationManager实例。

我尚未完全测试的另一种选择是尝试在主线程中强制创建和释放locationManager,如下所示:

class WorkoutLocationManager: NSObject {
var locationManager: CLLocationManager!
override init() {
super.init()
self.performSelector(onMainThread: #selector(initLocationManager), with: nil, waitUntilDone: true)
}
@objc private func initLocationManager() {
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
}
@objc private func deinitLocationManager() {
locationManager = nil
}
deinit {
self.performSelector(onMainThread: #selector(deinitLocationManager), with: nil, waitUntilDone: true)
}
}

让我知道你的想法。

相关内容

最新更新