主线程检查器:在后台线程上调用的 UI API:-[UIApplication applicationState] [英] Main Thread Checker: UI API called on a background thread: -[UIApplication applicationState]

查看:42
本文介绍了主线程检查器:在后台线程上调用的 UI API:-[UIApplication applicationState]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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

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

 覆盖 func viewDidLoad() {让 locationManager = CLLocationManager()locationManager.requestAlwaysAuthorization()locationManager.requestWhenInUseAuthorization()如果 CLLocationManager.locationServicesEnabled() {locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeterslocationManager.startUpdatingLocation()}viewMap.delegate = self让相机 = GMSCameraPosition.camera(withLatitude: 53.7931183329367, longitude: -1.53​​649874031544, zoom: 17.0)viewMap.animate(to: 相机)}func locationManager(manager: CLLocationManager, didUpdateLocations 位置: [CLLocation]) {让 locValue:CLLocationCoordinate2D = manager.location!.coordinateprint("locations = (locValue.latitude) (locValue.longitude)")}func mapView(_ mapView: GMSMapView, willMove 手势: Bool) {}func mapView(_ mapView: GMSMapView, idleAt 位置: GMSCameraPosition) {如果(移动> 1){移动 = 1UIView.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()},完成:无)}移动 = 1}//相机改变位置,这个方法每次都会调用func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {移动 = 移动 + 1如果(移动 == 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()},完成:无)}DispatchQueue.main.async {打印(移动:(移动)纬度:(self.viewMap.camera.target.latitude)")打印(移动:(移动)经度:(self.viewMap.camera.target.longitude)")}}

解决方案

首先,确保您对 google maps 和 ui 更改的调用是从主线程调用的.

您可以使用以下步骤在 Xcode 中启用

您可以使用以下内容在主线程上放置违规行:

DispatchQueue.main.async {//在这里做UI代码.//调用谷歌地图方法.}

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

对于问题:为什么会发生这种情况?"我认为 Apple 为边缘案例添加了一个 assertion,然后 Google 不得不更新他们的豆荚.

I am using google maps in Xcode 9 beta, iOS 11.

I am getting an error outputted to the log as follows:

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

Why would this be occurring as I am almost certain I'm not altering any interface elements from the main thread in my code.

 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)")
        }
    }

解决方案

First, make sure your invocations of google maps and ui changes are called from the main thread.

You can enable Thread Sanitizer option in Xcode using below steps:

You can put offending lines on the main thread with the following:

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

Also, update your current version of google maps. Google maps had to make a couple of updates for the thread checker.

For the question: "Why would this be occurring?" I think Apple added an assertion for an edge case which Google then had to update their pod for.

这篇关于主线程检查器:在后台线程上调用的 UI API:-[UIApplication applicationState]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆