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