主线程检查器:在后台线程上调用的UI API: - [UIApplication applicationState]

时间:2017-06-26 20:02:16

标签: swift google-maps xcode9-beta

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

我收到错误输出到日志中,如下所示:

  

主线程检查器:在后台线程上调用UI API: - [UIApplication applicationState]   PID:4442,TID:837820,主题名称:com.google.Maps.LabelingBehavior,队列名称: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)")
        }
    }

6 个答案:

答案 0 :(得分:94)

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

  1. 选择编辑方案 - >诊断,勾选主线程检查器和暂停问题。 enter image description here
  2. 运行iOS应用程序以重现此问题。 (Xcode应该在第一个问题上暂停。) enter image description here

  3. DispatchQueue.main.async {}中包装修改用户界面的代码 enter image description here

答案 1 :(得分:45)

尝试在DispatchQueue.main.async {}中包装修改用户界面的所有代码行。

答案 2 :(得分:4)

首先,请确保使用以下方法从主线程调用Google地图和ui更改的调用:

DispatchQueue.main.async {
    //Do UI Code here. 
    //Call Google maps methods.
}

此外,更新谷歌地图。谷歌地图必须为线程检查器做一些更新。

答案 3 :(得分:4)

请参阅此链接 https://developer.apple.com/documentation/code_diagnostics/main_thread_checker

对我来说,当我从街区打来电话时,这种方法很有用。

答案 4 :(得分:0)

我认为解决方案已经给出, 对于我的问题是键盘在路上。

UIKeyboardTaskQueue may only be called from the main thread

答案 5 :(得分:-18)

选择方案 - >诊断,删除主线程检查器,然后警告将消失。 scheme editor

相关问题