Code Block @IBAction func searchPlace(_ sender: Any) { nearBy() } func nearBy() { var googleURLAPI = URLComponents(string: "https://maps.googleapis.com/maps/api/place/nearbysearch/json")! googleURLAPI.queryItems = [ URLQueryItem(name: "location", value: "\(origin.latitude),\(origin.longitude)"), URLQueryItem(name: "radius", value: "15000"), URLQueryItem(name: "type", value: "Fast Food"), URLQueryItem(name: "keyword", value: "Food"), URLQueryItem(name: "key", value: "Key"), ] print(googleURLAPI.url!) var urlRequest = URLRequest(url: googleURLAPI.url!) urlRequest.httpMethod = "GET" let task = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in do { if let error = error { throw error } guard let data = data else { print("data is nil") return } //# For debugging, show response data as text //print(String(data: data, encoding: .utf8) ?? "?") guard let jsonDict = try JSONSerialization.jsonObject(with: data) as? [String: Any] else { print("response data is not a JSON object") return // or throw some error } //setting jsonDict to read datta from url //# Better check "status" guard let status = jsonDict["status"] as? String, status == "OK" else { print("API error, status is not OK") return // or throw some error } guard let results = jsonDict["results"] as? [[String: Any]] else { print("`results` is not an Array of JSON object") return // or throw some error } //print("or here",LocArray) for result in results { guard let name = result["name"] as? String else { print("value for `name` not found or not object") return // or throw some error or ignore and continue } guard let geometry = result["geometry"] as? [String: Any] else { print("value for `geometry` not found or not object") return // or throw some error or ignore and continue } guard let location = geometry["location"] as? [String: Double] else { print("value for `location` not found or not object") return // or throw some error or ignore and continue } guard let lat = location["lat"], let lng = location["lng"] else { print("value for `lat` or `lng` not found or not number") return // or throw some error or ignore and continue } self.nameArray.append(name) let coord = CLLocationCoordinate2D(latitude: lat, longitude: lng) self.locationArray.append(coord) } } catch { print(error) let title = NSLocalizedString("There was an Error", comment: "") let message = NSLocalizedString("We encountered an error while trying to connect to Google. Try again later.", comment: "") let alert = UIAlertController(title: title, message: message, preferredStyle: .alert) alert.addAction(UIAlertAction(title: "Okay!", style: .default)) self.present(alert, animated: true, completion: nil) } } task.resume() printNames() //gotoPlaces() } func printNames() { print("name array",nameArray) print("location array",locationArray) }
Have I somehow cleared the arrays without knowing?
Then you call it from the main thread:When I call the function gotoPlaces() after line 81 I get a crash and error that says:
Terminating app due to uncaught exception 'GMSThreadException', reason: 'The API method must be called from the main thread'
Code Block do { if let error = error { throw error } //... guard let lat = location["lat"], let lng = location["lng"] else { print("value for `lat` or `lng` not found or not number") return // or throw some error or ignore and continue } DispatcheQueue.main.async { self.nameArray.append(name) let coord = CLLocationCoordinate2D(latitude: lat, longitude: lng) self.locationArray.append(coord) self.gotoPlaces() } } catch { //... }
Solve the issue as suggested in the error message.