0

When i press the UIbutton the purpple error saying "UIViewController.navigationController must be used from main thread only" shows up. I am kinda new to this language so can any one tell me why an how to solve this problem?

@IBAction func searchButtonPressed(_ sender: UIButton) {
        
        let houseNo = field.text
        let wardNo = wardnoDropdown.text
        let afterURL =  "house_no=\(houseNo ?? "")&ward_no=\(wardNo ?? "")"
        let escapedString = afterURL.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed) ?? ""
        let finalURL = "\(searchURL)\(escapedString)"
        let nc = self.storyboard?.instantiateViewController(identifier: "MapViewController") as! MapViewController
        
        func performRequest(){
            //1.create URL
            if let url = URL(string: finalURL){
                //2.Create URL session
                let session = URLSession(configuration: .default)
                //3.Give session a Task
                let task = session.dataTask(with: url) { (data, response, error) in
                    if error != nil{
                        print("error")
                        return
                    }
                    if let safeData = data{

                        let decoder = JSONDecoder()
                        do{
                            let decodedData = try decoder.decode(CordinateData.self, from: safeData)
                            let lat = decodedData.data[0].x
                            let lon = decodedData.data[0].y
                            let doubleLat = Double(lat) ?? 0.0
                            let doubleLon = Double(lon) ?? 0.0
                            nc.lon = doubleLon
                            nc.lat = doubleLat
                            self.navigationController?.pushViewController(nc, animated: true)
                            //self.present(nc, animated: true, completion: nil)
                            print(doubleLat)
                            print(doubleLon)
                            
                        }catch{
                            print("error")
                        }
                    }
                }
                //Start the task
                task.resume()
            }
        }
        performRequest()
        
    }

Here the picture of the error

  • just write. "DispatchQueue.main.async { self.navigationController?.pushViewController(nc, animated: true) } " in place of self.navigationController?.pushViewController(nc, animated: true) – Noor Ahmed Natali Nov 02 '21 at 08:06
  • That purple color indicates that you have a retain cycle. What else would you find if you ask a search engine about the exact warning message? – El Tomato Nov 02 '21 at 08:14

1 Answers1

2

Add your view controller to to navigationController from main thread as -

    do{
        let decodedData = try decoder.decode(CordinateData.self, from: safeData)
        let lat = decodedData.data[0].x
        let lon = decodedData.data[0].y
        let doubleLat = Double(lat) ?? 0.0
        let doubleLon = Double(lon) ?? 0.0
        nc.lon = doubleLon
        nc.lat = doubleLat
        DispatchQueue.main.async {
             self.navigationController?.pushViewController(nc, animated: true)
            //self.present(nc, animated: true, completion: nil)
        }
                                
        print(doubleLat)
        print(doubleLon)
                                
    }catch{
        print("error")
    }
Imran0001
  • 580
  • 4
  • 11