code

메인 스레드 검사기 : 백그라운드 스레드에서 호출 된 UI API :-[UIApplication applicationState]

codestyles 2020. 9. 3. 19:22
반응형

메인 스레드 검사기 : 백그라운드 스레드에서 호출 된 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)")
        }
    }

먼저 다음을 사용하여 기본 스레드에서 Google지도 및 UI 변경을 호출했는지 확인합니다.

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

또한 현재 버전의 Google지도를 업데이트하세요. Google지도는 스레드 검사기를 위해 몇 가지 업데이트를해야했습니다.

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.


It's hard to find the UI code which is not executed in main thread sometimes. You can use the trick below to locate it and fix it.

  1. Choose Edit Scheme -> Diagnostics, tick Main Thread Checker and Pause on issues. enter image description here
  2. Run your iOS application to reproduce this issue. (Xcode should pause on the first issue.) enter image description here

  3. Wrap the code that modify the UI in DispatchQueue.main.async {} enter image description here


Wrap the lines of code that modify the UI in DispatchQueue.main.async {} in order to make sure they execute on the main thread. Otherwise, you may be calling them from a background thread, where UI modifications are not allowed. All such lines of code must be executed from the main thread.


Refer this link https://developer.apple.com/documentation/code_diagnostics/main_thread_checker

For me this worked when I called from block.


I think the solution is already given, For my issue is Keyboard on the way.

UIKeyboardTaskQueue may only be called from the main thread


Choose scheme -> Diagnotics, remove main thread checker, then the warning will disappear. scheme editor

참고URL : https://stackoverflow.com/questions/44767778/main-thread-checker-ui-api-called-on-a-background-thread-uiapplication-appli

반응형