RxSwift 重试全链



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

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