【发布时间】:2022-10-23 21:12:30
【问题描述】:
从 iOS 16/Xcode 14 开始,我收到此错误:
如果在主线程上调用此方法,可能会导致 UI 无响应。相反,请考虑等待 -locationManagerDidChangeAuthorization: 回调并首先检查授权状态。”?
我正在观察滚动冻结和长按冻结。
苹果建议的应该怎么做?
这是我当前的代码段
/In ViewDidLoad
if CLLocationManager.locationServicesEnabled() {
let authorizationStatus: CLAuthorizationStatus
if #available(iOS 14, *) {
authorizationStatus = locationManager.authorizationStatus
} else {
authorizationStatus = CLLocationManager.authorizationStatus()
}
switch authorizationStatus {
case .authorizedAlways, .authorizedWhenInUse:
locationManager.delegate = self
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.startUpdatingLocation()
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.allowsBackgroundLocationUpdates = true
//////here data loading happens too////////////
case .notDetermined:
case .restricted:
case .denied:
@unknown default:
print("Location services are not enabled")
}
/outside ViewDidLoad
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
///location database related stuff
}
我按照这里的建议尝试了 async/await ,但它没有解决问题。 https://developer.apple.com/forums/thread/714467
【问题讨论】:
标签: ios swift objective-c cllocationmanager cllocation