2016-01-18 61 views
0

我很难理解完成处理程序。我试图让一个函数(purchaseRequest)等待,直到另一个函数(didReceiveResponse)不被我调用,而是作为委托完成。有人能给我一个关于如何实现这一目标的指针吗?在委托方法中添加完成处理程序

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) 
} 

回答

1

•推进度微调视图控制器

•使purchaseRequest后立即返回requestProductInfo()

requestProductInfo()后的委托方法捕获代码的新方法

•,dispatch_async()到主队列并调用新方法

•在新方法中,弹出微调视图控制器,然后进行高级购买舞蹈

+1

谢谢!我所做的是调用requestProductInfo(),然后使用dispatch_async()在didReceiveResponse委托内部进行purchaseRequest! –

+0

@JacoboKoenig太棒了!小心“一路阻挡”陷阱;回调处理程序调用异步方法传递回调处理程序,该回调处理程序使用回调处理程序调用另一个方法。 (我充满罪责)。在某个时候,您想要将回调处理程序重构为自己的方法,然后调用它。 – bbum

相关问题