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

Mat*_*ack 94 google-maps swift 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)")
        }
    }
Run Code Online (Sandbox Code Playgroud)

Unc*_*rks 142

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

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

  3. 包含修改UI的代码 DispatchQueue.main.async {} 在此输入图像描述

  • 出于某种原因,当我在Xcode 10.1中执行此操作时,我只会得到一个无助的调用栈,并且无法与代码行相关:2018-12-29 19:46:56.500629 + 0100 BedtimePrototype [1553:834478] [报告]主线程检查器:在后台线程上调用的UI API:-[UIApplication applicationState] PID:1553,TID:834478,线程名称:com.apple.CoreMotion.MotionThread,队列名称:com.apple.root.default-qos。过量使用,QoS:0 (9认同)
  • 谢谢,救我30分钟. (4认同)
  • 我还通过注释掉SVProgressHUD.show摆脱了警告。将此调用包装到DispatchQueue.main.async中并没有摆脱警告。我在v2.2.5中使用了pod。此线程可以帮助更好地理解问题:https://github.com/SVProgressHUD/SVProgressHUD/issues/950 (2认同)
  • 在 xcode 11.4.1 中,“问题暂停”复选框不适合我。编辑:我在主线程检查器旁边发现了一个小箭头。单击该按钮会为您添加一个断点。 (2认同)
  • 这似乎在 Xcode 12 中消失了。有人知道它移到哪里了吗? (2认同)

Dim*_*dui 46

尝试包装修改UI的所有代码行DispatchQueue.main.async {}.

  • 是的我已经使用过,但警告仍然存在 (4认同)
  • @MattBlack尝试在`DispatchQueue.main.async {}上包装修改UI的所有内容. (4认同)

Sco*_*des 27

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

DispatchQueue.main.async {
    //Do UI Code here. 
    //Call Google maps methods.
}
Run Code Online (Sandbox Code Playgroud)

另外,更新您当前版本的Google地图。Google地图必须对线程检查器进行一些更新。

问题是:“为什么会这样?” 我认为苹果为一个边缘案例添加了一个断言,然后谷歌不得不为其更新pod。

  • Xcode 9突出显示了xcode 8显然无法检测到的一些线程问题(无论是由于xcode设置还是要确定另一件事)。切换回xcode 8等效于忽略线程问题,这些问题仍然存在并且没有解决,因此这不是解决方案,您只是将头埋在沙子里并假装一切正常。如果问题来自框架,请提交问题,以便可以解决。 (6认同)
  • 是的...我认为对于许多ios开发人员来说,仅因为某些东西正在显示症状就很难看到它,但这并不意味着它也不是原因。我认为也很难接受苹果可能曾经犯过的错误。 (2认同)