RxSwift: Use Zip with different type observables

前端 未结 3 1807
误落风尘
误落风尘 2021-02-14 05:28

I\'m using RxSwift 2.0.0-beta

How can I combine 2 observables of different types in a zip like manner?

// This works
[just(1), just(1)].zip { intElement         


        
3条回答
  •  日久生厌
    2021-02-14 05:57

    If you using Singles (RxSwift 5.0+)

    Single.zip(single1, single2) {
        return ($0, $1)
    }
    

    or

    Observable.zip(single1.asObservable(), single2.asObservable()) {
        return ($0, $1)
    }
    

    Example:

    let task = Single
                    .zip(repository.getArrayA(), repository.getArrayB())
                    { (a: [A], b: [B]) in
                        return (a, b)
                    }                    
                    .do(onSuccess: { (zip) in
                    let (a, b) = zip
    
                    // your code
    
                    //example: self.updateSomething(a, b)
    
                })
                .asCompletable()
    

提交回复
热议问题