我正在尝试使用RxSwift Observables进行递归调用。
import RxSwift
func observeUntil(initialValue: Int) -> Observable<Int> {
return Observable.deferred {
.just(initialValue)
}
.do(onNext: {
print("current item is", $0)
})
.flatMapLatest{ (item) -> Observable<Int> in
if item < 5 {
return Observable.just(item)
// .delay(.seconds(1), scheduler: MainScheduler.instance)
.flatMapLatest{observeUntil(initialValue: $0 + 1)}
} else {
return .just(item)
}
}
}
_ = observeUntil(initialValue: 0)
.subscribe()
当我在上面的代码中注释延迟时,输出如下所示正确显示
current item is 0
current item is 1
current item is 2
current item is 3
current item is 4
current item is 5
Program ended with exit code: 0
带延迟代码仅输出
current item is 0
Program ended with exit code: 0
[请帮助我了解添加延迟后会发生什么。
_ = observeUntil(initialValue: 0).subscribe()