Reputation: 5396
I have problem with adopting RxSwift to my application. My problem is: If I have many input signals (Variables with different elements) that can be change by user and from this changes I want to build one Observable signal. I think I should use some sort of Combination operator but for example combineLatest only allows for two parameters to combine.
Let's look at example of inputs and outputs that I have.
import RxSwift
import RxCocoa
class ModelView {
private let api: ApiType
// inputs
let inType: Variable<MWItemQueryType>
let inFilterRetailers: Variable<Set<MWRetailer>>
let inFilterColors: Variable<Set<MWColor>>
let inFilterPriceRanges: Variable<Set<MWPriceRange>>
// outputs
let outQuery: Observable<MWItemQuery>
init(initialType: MWItemQueryType, api: ApiType) {
self.api = api
self.inType = Variable(initialType)
self.inFilterRetailers = Variable(Set<MWRetailer>())
self.inFilterColors = Variable(Set<MWColor>())
self.inFilterPriceRanges = Variable(Set<MWPriceRange>())
// TODO How to setup outQuery signal here
}
}
So I have problem with setup outQuery that emits signals MWItemQuery
. The MWItemQuery
signal must be emitted when one of the input parameters changed. The MWItemQuery
is then emitted to the api to fetch data from server with the most recent filters selected by the user. I have a lot of other input filter signals and I don't know if any operator can help me.
Upvotes: 1
Views: 4149
Reputation: 2326
combineLatest
can accept more than 2 parameters!
Try:
Observable.combineLatest(inType.asObservable(), inFilterRetailers.asObservable(), inFilterColors.asObservable(), inFilterPriceRanges.asObservable())
{ type, filterRetailers, filterColors, filterPriceRanges in
// do something with your data
}
.distinctUntilChanged()
.shareReplay(1)
Upvotes: 5
Reputation: 1882
You can use combineLatest operator, it can combine 2..8 observables
.
There are different version to take different number of observables
.
Upvotes: 1