我在Xcode 9 beta版iOS 11中使用谷歌 map .

我得到一个输出到日志(log)的错误,如下所示:

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

为什么会发生这种情况,因为我几乎可以肯定我没有改变代码中主线程的任何接口元素.

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

推荐答案

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

您可以使用以下步骤在Xcode中启用Thread Sanitizer选项:

Screenshot

您可以使用以下命令在主线程上添加不符合要求的行:

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

此外,请更新您当前的谷歌 map 版本.谷歌 map 不得不对线程判断器进行几次更新.

对于问题:"为什么会发生这种情况?"我认为苹果在edge手机壳上加了assertion分,然后谷歌不得不更新他们的pod.

Swift相关问答推荐

避免嵌套导航TabView

为什么枚举上的.allCases.forEach不是循环(继续和中断不起作用)?

如何使用AsyncTimerSequence获取初始时钟,然后在指定的时间间隔内开始迭代?

如何获取嵌套数组中项的路径以修改数组?

如何在设备(或常量)地址空间中创建缓冲区?

我如何遵守与演员的协议?

RxSwift 事件触发了两次

Swift - 给定一个像 30 的 Int 如何将范围数乘以 7?

在 SwiftUI 中的 ForEach 中使用 id 时如何为数组设置动画索引

令人满意的 var body:协议扩展中的一些视图

Swift Components.Url 返回错误的 URL

异步等待不会在项目中触发,但在 Xcode playground 中工作正常

在 xcode 13 中的构建之间保持可访问性权限

Swift 中的 PageViewController 当前页面索引

iOS:检测设备是否为 iPhone X 系列(无框)

更改日期 Select 器的文本 colored颜色

修复 Swift 3 中的警告C-style for Statement is deprecated

更改在变量的 willSet 块中设置的值

使用 ARAnchor 插入 node 和直接插入 node 有什么区别?

Void 函数中意外的非 Void 返回值 (Swift 2.0)