RxSwift重试完整链

时间:2018-06-13 18:03:16

标签: swift rx-swift

我在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。 :(

1 个答案:

答案 0 :(得分:0)

确保您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永远不会被调用,因为它基于第一流的输出。

相关问题