【问题标题】:PromiseKit go back one stepPromiseKit 后退一步
【发布时间】:2017-11-13 16:42:59
【问题描述】:

我的应用中有一系列 UIViewController(弹出窗口),用于从最终用户那里收集信息。我设法用这样的承诺将它们链接起来:

firstly {
        return showFirstPopup()
    }
    .then { info1 -> Promise<Info2> in
        dismissFirstPopup()
        //Do sth with info1...
        return showSecondPopup()
    }
    .then { info2 -> Promise<Info3> in
        dismissSecondPopup()
        //Do sth with info2...
        return showThirdPopup()
    }
    .then { info3 -> Promise<Info4> in
        dismissThirdPopup()
        //Do sth with info3...
        return showForthPopup()
    }
    ...
    .catch { error in
        //Handle any error or cancellation...
     }

例如,如果用户在第三个弹出窗口中按回,我需要返回到上一个“then”而不是取消整个流程。

基本上我需要能够后退一步,让用户编辑数据,然后继续流程。

有没有办法用 PromiseKit 做到这一点?

【问题讨论】:

    标签: ios swift promisekit


    【解决方案1】:

    这就是我最终使用的。希望这对其他人也有帮助。

    func myFunc(info: Info) -> Promise<()>
    {
        return Promise { fulfill, reject in
    
            let firstPromise = shouldShowFirstPopup ? showFirstPopup() : Promise(value: info.info1)
    
            firstPromise
            .then { info1 -> Promise<Info2> in
                info.info1 = info1
    
                if (info.shouldShowSecondPopup) {
                    return showSecondPopup()
                }
    
                return Promise(value: info.info2)
            }
            .then { info2 -> Promise<Info3> in
                info.info2 = info2
                info.shouldShowSecondPopup = false
    
                if (info.shouldShowThirdPopup) {
                    return showThirdPopup()
                }
    
                return Promise(value: info.info3)
            }
            .then { info3 -> Promise<()> in
                info.info3 = info3
                info.shouldShowThirdPopup = false
    
                return processEverything()
            }
            .then { _ -> () in
                fulfill(())
            }
            .catch { error in
                switch error {
                    case MyErrors.backPressed(let popupType):
                        switch popupType {
                            case .firstPopup:
                            reject(MyErrors.userCanceled)
                            return
    
                            case .secondPopup:
                            info.shouldShowFirstPopup = true
                            info.shouldShowSecondPopup = true
    
                            case .thirdPopup:
                            info.shouldShowSecondPopup = true
                            info.shouldShowThirdPopup = true
    
                            default:
                            reject(MyErrors.defaultError(message: "Not implemented case exception"))
                            return
                        }
    
                        firstly {
                            return myFunc(info: info)
                        }
                        .then { _ -> () in
                            fulfill(())
                        }
                        .catch { error2 in
                            reject(error2)
                        }
    
                    default:
                    reject(error)
                }
            }
        }
    } 
    

    【讨论】:

      【解决方案2】:

      这是我对 PromiseKit 的发现:

      PromiseKit 内置了取消的概念。如果用户取消了某事,那么通常你不想继续承诺链,但你也不想显示错误消息。那么什么是取消呢? 这不是成功也不是失败,但它应该更像一个错误来处理,即。它应该跳过所有后续的 then 处理程序。 PromiseKit 将取消体现为一种特殊的错误。

      据此,您将跳过所有后续的 then 处理程序。 这是我找到此信息的链接:Promise Kit

      【讨论】:

      • 谢谢,但我不想取消流程。我需要能够后退一步。我意识到我最初的问题不是很清楚。我现在已经更新了。
      猜你喜欢
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2014-05-17
      • 2019-08-07
      • 2016-11-11
      • 1970-01-01
      • 2023-04-02
      • 2021-03-16
      相关资源
      最近更新 更多