メインスレッドチェッカー: バックグラウンドスレッドで呼び出されたUI API: -[UIApplication applicationState] 質問する

メインスレッドチェッカー: バックグラウンドスレッドで呼び出されたUI API: -[UIApplication applicationState] 質問する

私は Xcode 9 ベータ版、iOS 11 で Google マップを使用しています。

ログに次のようなエラーが出力されます:

メイン スレッド チェッカー: バックグラウンド スレッドで 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)")
        }
    }

ベストアンサー1

メインスレッドで実行されない UI コードを見つけるのは難しい場合があります。以下のトリックを使用して、それを見つけて修正することができます。

  1. 「スキームの編集」->「診断」を選択し、「メイン スレッド チェッカー」にチェックを入れます。

    11.4.1 の

    メイン スレッド チェッカーの横にある小さな矢印をクリックして、メイン スレッド チェッカー ブレークポイントを作成します。ここに画像の説明を入力してください

    以前の Xcode

    問題が発生した場合は一時停止にチェックを入れます。ここに画像の説明を入力してください

  2. この問題を再現するには、iOS アプリケーションを実行します (最初の問題では Xcode が一時停止するはずです)。ここに画像の説明を入力してください

  3. UIを変更するコードをラップするDispatchQueue.main.async {} ここに画像の説明を入力してください

おすすめ記事