Can someone tell me why my segue passes old data? With old I mean the text "test" gets passed and not the new variable for idForPlace which first retrieves its value from an API request. Do I need to use a completion handler or manually prepare the segue after changing the value of idForPlace?
...
var idForPlace = "test"
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
loadingInitialMap()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Fenster1" {
let destinationVC = segue.destination as? ViewComments
destinationVC?.idForPlace = idForPlace
}
}
func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
if marker.title == nil {
print("Kein Marker vorhanden")
} else {
print("Vorhandene Marker angeklickt")
ViewController.titelUebertragen = marker.title!
let data = marker.userData as? [String:String]
var idForPlace = (data?["id"])!
let ref = Database.database().reference()
ref.child("placeID/\(idForPlace)").observeSingleEvent(of: .value, with: { (snapshot) in
print(idForPlace)
self.performSegue(withIdentifier: "Fenster1", sender: marker)
})
}
return true
}
...
>Solution :
Because you have a local variable in mapView that you update
var idForPlace = (data?["id"])!
but what you use in your prepare(for:sender:) is the property defined for the class
destinationVC?.idForPlace = idForPlace
so remove the local variable and instead do
self.idForPlace = (data?["id"])!