Код: Выделить всё
import RxSwift
func observeUntil(initialValue: Int) -> Observable {
return Observable.deferred {
.just(initialValue)
}
.do(onNext: {
print("current item is", $0)
})
.flatMapLatest{ (item) -> Observable 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
Подробнее здесь: https://stackoverflow.com/questions/589 ... bservables