有人可以告诉我是否可以在concat运算符中创建一个递归补全。
我想获取一个会话,并在此之后为用户加载相应的会话ID。
SessionAPI.post(email: email, password: password)
UserAPI.get(id: Session.load()!.userId)
直到现在,我将可观察对象与flatMap运算符一起使用。
我现在将尝试使用completables来重现相同的行为,而completables没有flatMap运算符。
带有可观察对象的工作代码:
SessionAPI.post(email: email, password: password)
.flatMap { (_) -> Single<Any> in
return UserAPI.get(id: Session.load()!.userId)
}
带有补充内容的新工作代码
SessionAPI.post(email: email, password: password)
.concat(Completable.deferred { UserAPI.get(id: Session.load()!.userId) } )
我现在想为此延期的补全创建扩展,例如:
SessionAPI.post(email: email, password: password)
.concatDeferred(UserAPI.get(id: Session.load()!.userId))
当前扩展名:
extension PrimitiveSequenceType where Self.Element == Never, Self.Trait == RxSwift.CompletableTrait {
func concatDeferred(_ second: RxSwift.Completable) -> RxSwift.Completable {
return Completable.deferred { () -> PrimitiveSequence<CompletableTrait, Never> in
return second
}
}
}
问题: Session.load()!在SessionAPI.post完成之前,UserAPI.get中的文件已加载并崩溃。
有人知道如何运行此扩展程序吗?
谢谢!
我将假设您要推迟UserAPI.get(id:)
的原因是,在SessionAPI.post(email:password:)
使之生效的背景中发生了一些“魔术”,因此Session.load()
有效。
这告诉我,post(email:password:)
首先应该是[[not可完成的。相反,它应该返回Observable<T>
,其中T是Session.load()返回的值。
无法
使代码像您想要的那样工作:SessionAPI.post(email: email, password: password)
.concatDeferred(UserAPI.get(id: Session.load()!.userId))
使用上述代码,无论您在在Session.load()
中输入什么代码,都将在SessionAPI.post(email:password:)
被调用之前调用concatDeferred
。
Session.load()
函数必须
concatDeferred
之前被调用,以便前者可以将其结果传递给后者。