I am trying to wait on an asynchronously performing class to finish before continuing but are not having luck. Key points:
- class CLGeocoder that performs asynchronously is called from an IBAction
- I would like to set a value in the CLGeocoder completion handler and pass it to another view controller via prepareForSegue
- What is happening: the IBAction is finishing before the CLGeocoder completion handler finishes, so prepareForSegue is called before the value is set
I have looked at: 24725059 and especially good 25634068 but not been able to craft an answer for my situation.
Most answers say in essence to put the logic to be executed in a completion handler but the logic that I want to ultimately perform is prepareForSegue.
I am just learning, so I this could very well be due to my lack of understanding of completion handlers among other concepts.
var locationPlacemark: CLPlacemark!
@IBOutlet weak var myLocation: UITextField!
@IBAction func myButton(sender: AnyObject) {
print("start button IBAction")
processGeo({ (placemark, error) -> Void in
self.locationPlacemark = placemark
print("setting placemark")
})
print("end button IBAction")
}
func processGeo(getLocCompletionHandler: ((placemark : CLPlacemark?, error : NSError?) -> Void)!) {
print("before CLGeocoder")
CLGeocoder().geocodeAddressString(self.myLocation.text!, completionHandler: {(placemarks, error) -> Void in // called after completes
if((error) != nil){
print("Error", error)
}
print("after CLGeocoder")
if let placemark = placemarks?[0] {
getLocCompletionHandler(placemark: placemark, error: error)
print("after calling completion handler")
}
})
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "InfoPostingMapSegue" {
let vc = segue.destinationViewController as! InfoPostingMapViewController
print("in prepareForSegue")
vc.locationPlacemark = locationPlacemark
}
}
The console prints are:
start button IBAction
before CLGeocoder
end button IBAction
in prepareForSegue
after CLGeocoder
setting placemark
after calling completion handler