Ios 在委托方法上添加完成处理程序
我很难理解完成处理程序。我试图让一个函数(purchaseRequest)等待另一个函数(didReceiveResponse)完成,该函数不是我调用的,而是作为一个委托调用的。有人能给我一个如何实现这一点的指点吗Ios 在委托方法上添加完成处理程序,ios,swift,completionhandler,Ios,Swift,Completionhandler,我很难理解完成处理程序。我试图让一个函数(purchaseRequest)等待另一个函数(didReceiveResponse)完成,该函数不是我调用的,而是作为一个委托调用的。有人能给我一个如何实现这一点的指点吗 func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) { print("response received") if resp
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("response received")
if response.products.count != 0 {
productsArray = response.products[0]
print(productsArray)
} else { print("No Products Found") }
}
func purchaseRequest() {
requestProductInfo()
//NEED TO HOLD UNTIL didReceiveResponse (which, as a delegate method, is not called by me) IS DONE.
print("Product1: \(self.productsArray)")
let aSC = UIAlertController(title: "Premium App Required", message: "Premium App is Required for this feature. Would you like to purchase it for $0.99?", preferredStyle: UIAlertControllerStyle.ActionSheet)
let buyAction = UIAlertAction(title: "Purchase", style: UIAlertActionStyle.Default) { (action) -> Void in
let payment = SKPayment(product: self.productsArray)
SKPaymentQueue.defaultQueue().addPayment(payment)
}
let cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) { (action) -> Void in
}
aSC.addAction(buyAction)
aSC.addAction(cancelAction)
self.presentViewController(aSC, animated: true, completion: nil)
}
•推送进度微调器视图控制器 •在
requestProductInfo()之后立即返回purchaseRequest
•以新方法捕获requestProductInfo()之后的代码
•在委托方法中,dispatch_async()
到主队列并调用新方法
•在新方法中,弹出微调器视图控制器,然后跳高级购买舞蹈谢谢!我所做的是调用requestProductInfo(),然后使用dispatch_async()在didReceiveResponse委托中生成purchaseRequest@雅各布:太好了!小心“一路下来都是积木”的陷阱;调用异步方法的回调处理程序,该异步方法传入一个回调处理程序,该回调处理程序使用回调处理程序调用另一个方法。(我被指控有罪)。在某种程度上,您希望将回调处理程序重构为自己的方法,然后调用该方法。