Search code examples
iosswiftrx-swift

How to pass multiple parameters with using RxSwift?


I did with below code for passing single parameter.

lazy var priceListData: Observable<FoodPrice> = {
        return self.foodNamesparams1.asObservable()
                   .flatMapLatest(ApiCallViewModel.foodList(_:))
}

But I don't know how to pass multiple parameters..
If I need to pass foodNamesparams1, foodNamesparams2 to ApiCallViewModel.foodList
How can I do it with RxSwift?

foodNamesparams1 and foodNamesparams2 both are BehaviorRelay


Solution

  • You most likely want to combine the parameters first, most likely via combineLatest or zip.

    This will looks something like this

    Observable.combineLatest(
        foodNamesparams1.asObservable(),
        foodNamesparams2.asObservable()
      )
      .flatMapLatest(ApiCallViewModel.foodList)
    
    

    CombineLatest will not emit anything unless both combined observables have emitted at least once - and then it will commit on any update of either combined observable.

    Check out the combining section here http://reactivex.io/documentation/operators.html#combining to find the best suited combination method