RxSwift - how to chain observables sequentially

后端 未结 1 1095
星月不相逢
星月不相逢 2021-01-03 05:24

Assume I have array of Ints:

var items = [1, 2, 3, 4, 5]

and a function that takes Int argument and basing on it sends network request:

相关标签:
1条回答
  • 2021-01-03 05:43

    Operator .concatMap() guarantees that order remains intact, as opposed to .flatMap()

    i.e.

    Observable.from([1,2,3,4,5]).concatMap( /* your request code */ )
    

    UPDATE

    let source = Observable.from([1,2,3,4,5])
    let trigger = BehaviorSubject<Int>(value: 0)
    let feed = Observable.zip(source, trigger) { return $0.0 }
    
    feed.concatMap {
        /* your request code */
        trigger.onNext(0) // possibly in .onComplete of the service call
    }
    
    0 讨论(0)
提交回复
热议问题