我有一个异步函数(geocodeAddressString),它在准备segue函数时被调用。问题是这个异步函数在执行segue后返回。下面的异步函数将地址字符串转换为坐标。在此准备功能之前我不能调用这个异步函数,因为用户地址字段可以随时由用户编辑。当按下保存按钮时,我尝试调用异步函数,然后在异步函数返回时对segue进行编码,但按下的按钮和出现的继续之间的加载时间太长。在不同视图中快速捕捉异步函数
所以我的问题是,如果有什么方法可以在新视图中捕获这种异步方法?然后我可以用实际返回的值替换一些临时坐标。任何帮助表示赞赏,谢谢!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
//convert address to coordinates
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(addressTxt.text!) { (placemarks, error) in
guard
let placemarks = placemarks,
let location = placemarks.first?.location
else {
// handle no location found
return
}
let tempCoord = CLLocationCoordinate2D(latitude: (location.coordinate.latitude), longitude: (location.coordinate.longitude))
self.donatedItem = DonatedItem(self.titleTxt.text!, self.itemImg.image!, self.donated, self.descTxt.text!, expirationDate, tempCoord, (user?.uid)!, (self.donatedItem?.itemID)!, self.addressTxt.text!, reserved: false, reservedBy: "NA")
self.donatedItem?.updateItem()
}
switch(segue.identifier ?? "") {
case "saveItem":
homeViewController.isReturningSegue = true
homeViewController.tempItem = self.donatedItem
default:
print("Undefined segue")
}
}
感谢您的提示。我希望尽可能避免使用进度指示器,但在任何情况下,我都会记住这个框架,以防止加载不可避免。 –