gpt4 book ai didi

swift - 主线程检查器 : UI API called on a background thread: -[UIApplication applicationState]

转载 作者:IT王子 更新时间:2023-10-29 04:56:55 24 4
gpt4 key购买 nike

我在 Xcode 9 beta、iOS 11 中使用谷歌地图。

我收到如下错误输出到日志:

Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState] PID: 4442, TID: 837820, Thread name: com.google.Maps.LabelingBehavior, Queue name: com.apple.root.default-qos.overcommit, QoS: 21

为什么会发生这种情况,因为我几乎可以肯定我不会在我的代码中更改主线程中的任何界面元素。

 override func viewDidLoad() {

let locationManager = CLLocationManager()


locationManager.requestAlwaysAuthorization()


locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {

locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}

viewMap.delegate = self

let camera = GMSCameraPosition.camera(withLatitude: 53.7931183329367, longitude: -1.53649874031544, zoom: 17.0)


viewMap.animate(to: camera)


}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}

func mapView(_ mapView: GMSMapView, willMove gesture: Bool) {


}

func mapView(_ mapView: GMSMapView, idleAt position: GMSCameraPosition) {

if(moving > 1){
moving = 1
UIView.animate(withDuration: 0.5, delay: 0, animations: {

self.topBarConstraint.constant = self.topBarConstraint.constant + (self.topBar.bounds.height / 2)

self.bottomHalfConstraint.constant = self.bottomHalfConstraint.constant + (self.topBar.bounds.height / 2)

self.view.layoutIfNeeded()
}, completion: nil)
}
moving = 1
}


// Camera change Position this methods will call every time
func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
moving = moving + 1
if(moving == 2){


UIView.animate(withDuration: 0.5, delay: 0, animations: {


self.topBarConstraint.constant = self.topBarConstraint.constant - (self.topBar.bounds.height / 2)

self.bottomHalfConstraint.constant = self.bottomHalfConstraint.constant - (self.topBar.bounds.height / 2)


self.view.layoutIfNeeded()
}, completion: nil)
}
DispatchQueue.main.async {

print("Moving: \(moving) Latitude: \(self.viewMap.camera.target.latitude)")
print("Moving: \(moving) Longitude: \(self.viewMap.camera.target.longitude)")
}
}

最佳答案

有时很难找到不在主线程中执行的 UI 代码。您可以使用下面的技巧找到它并修复它。

  1. 选择 Edit Scheme -> Diagnostics,勾选 Main Thread Checker。

    Xcode 11.4.1

    单击 Main Thread Checker 旁边的小箭头以创建 Main Thread Checker 断点。 enter image description here

    以前的 Xcode

    勾选暂停问题。 enter image description here

  2. 运行您的 iOS 应用程序以重现此问题。 (Xcode 应该在第一个问题上暂停。) enter image description here

  3. 将修改 UI 的代码包装在 DispatchQueue.main.async {}enter image description here

关于swift - 主线程检查器 : UI API called on a background thread: -[UIApplication applicationState],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44767778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com