【问题标题】:PromiseKit unable to hit catch within chainPromiseKit 无法在链中命中
【发布时间】:2018-12-19 10:00:13
【问题描述】:

我目前正在混合使用 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)
            }
        }
    }
}

我已经设置了一个断点,并且当我触摸取消时会遇到错误情况,但这并不会在 Promise 链中触发 catch 块。我似乎无法说出原因。

【问题讨论】:

    标签: ios swift asynchronous promisekit


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-06
      • 2016-09-11
      • 2016-02-08
      • 2015-12-14
      • 1970-01-01
      • 2021-05-12
      • 1970-01-01
      • 2017-07-08
      相关资源
      最近更新 更多