PromiseKit无法击中链中的陷阱

时间:2018-12-19 10:00:14

标签: ios swift asynchronous promisekit

我目前正在混合使用SwiftyStoreKit和PromiseKit来处理应用内购买,我遇到的问题是,如果我在其中的一个中抛出错误,承诺链中的catch块就不会调用reject()函数时被执行/命中。

关于我如何链接这些承诺,您可以在下面看到。

     firstly {
        IAPViewModel.retrieveIAPProducts()
     }.then { products in
        IAPViewModel.purchase(products[1])
     }.ensure {
        // This is to silence the warning of values being unused
        _ = IAPViewModel.validatePurchases()
     }.catch { error in
        UIAlertController.show(message: "Error - \(error._code): \(error.localizedDescription)")
     }

包裹Promise的功能示例,最好的示例可能是我的购买功能,因为用户可以单击“取消”,这将引发错误。见下文。

static func purchase(_ product: SKProduct) -> Promise<Void> {

    let loftyLoadingViewContentModel =  LoftyLoadingViewContentModel(title: "Purchasing".uppercased(), message: "We’re currently processing your\nrequest, for your subscription.")
    UIApplication.topViewController()?.showLoadingView(.popOverScreen, loftyLoadingViewContentModel)

    return Promise { seal in

        SwiftyStoreKit.purchaseProduct(product) { purchaseResult in

            switch purchaseResult {
            case .success(let product):
                if product.needsFinishTransaction {
                    SwiftyStoreKit.finishTransaction(product.transaction)
                }
                seal.fulfill()
                log.info("Purchase Success: \(product.productId)")
            case .error(let error):
                UIApplication.topViewController()?.removeLoadingView()
                seal.reject(error)
            }
        }
    }
}

我已经设置了一个断点,当我触摸“取消”时就遇到了错误情况,但是这并不会触发Promises链中的catch块。我似乎无法对为什么这么做。

1 个答案:

答案 0 :(得分:1)

设法弄清楚了,我必须通过将此参数添加到块(policy: .allErrors)中来明确设置我希望捕获所有错误。

     firstly {
        IAPViewModel.retrieveIAPProducts()
     }.then { products in
        IAPViewModel.purchase(products[1])
     }.ensure {
        // This is to silence the warning of values being unused
        _ = IAPViewModel.validatePurchases()
     }.catch (policy: .allErrors) { error in
        UIAlertController.show(message: "Error - \(error._code): \(error.localizedDescription)")
     }