[英]how to wait for the URLSession to finish before returning the result from a function in Swift 3
[英]How to wait for function to finish and update tableview before segue. iOS Swift
我正在制作一个电子商务应用程序。 在购物篮视图控制器上,当用户点击结帐按钮时,我想检查商品的可用性并在例如库存不足时相应地更新购物篮,数量将更改为可用库存,或者当在转到另一个视图控制器之前没有可用的库存时,产品会从篮子中移除。 问题是,我有一个函数,它有一个完成处理程序,它应该只在函数完成时调用 performSegue 并且如果篮子没有变化,但是,它仍然会在函数完成之前转到另一个视图控制器. 有关如何正确执行此操作的任何建议?
我的完成处理程序没有正确完成,这就是它无法正常工作的原因。 但我终于让它与以下代码一起工作
var changes = Bool()
func getBasketItems(completion: @escaping(_ success:Bool) -> Void) {
//each time there's an update to the database I set changes to true
changes = true
completion(changes)
}
我用它作为下面的代码
self.getBasketItems { (success) in
if success {
// there are changes, update basket tableview
print("there are changes")
} else {
self.performSegue(withIdentifier: "goToMap", sender: self)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.