【问题标题】:RxSwift retry full chainRxSwift 重试全链
【发布时间】:2018-06-13 18:03:16
【问题描述】:

我是 RxSwift 的新手,遇到以下问题。

给定两个函数:

struct Checkout { ... }

func getSessionIdOperation() -> Single<UUID>
func getCheckoutForSession(_ sessionId: UUID, asGuestUser: Bool) -> Single<Checkout>

我有第三个函数,它结合了两者的结果:

func getCheckout(asGuestUser: Bool) -> Single<Checkout> {
    return getSessionIdOperation()
        .map { ($0, asGuestUser) }
        .flatMap(getCheckoutForSession)
}

getSessionIdOperationgetCheckoutForSession都可能失败,如果失败,我只想重新启动整个链一次。我尝试了retry(2),但只是重复了getCheckoutForSession。 :(

【问题讨论】:

    标签: swift rx-swift


    【解决方案1】:

    确保retry(2)flatMap 一起直播

    func getCheckout(asGuestUser: Bool) -> Single<Checkout> {
        return getSessionIdOperation()
            // .retry(2) will retry just first stream
            .map { ($0, asGuestUser) }
            .flatMap(getCheckoutForSession)
            .retry(2) // Here it will retry whole stream
    }
    

    如果getSessionIdOperation 失败,getCheckoutForSession 将永远不会被调用,因为它基于第一个流的输出。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多