Guven
Guven

Reputation: 2310

Create Multiple Observables from a Single one - RxSwift

I have relatively expensive operation so I am willing to perform that operation once and create 2 Observables from it.

Here is how it looks:

let outputObservable1: Observable<Bool>
let outputObservable2: Observable<Bool>

(outputObservable1, outputObservable2) = inputObservable1.zip(inputObservable2).map { booleanCondition1, booleanCondition2 in
   // different condition combinations create different outputObservables
}

I am guessing map is not the right operator here as it will only yield one observable. How can I mix and match the conditions and return 2 Observables at once?

Upvotes: 1

Views: 1417

Answers (1)

Daniel T.
Daniel T.

Reputation: 33967

Based on my understanding, you just need to use map

let inputs = Observable.zip(inputObservable1, inputObservable2)
    .share() // you only need this if one of your inputs is making a network request.

let outputObservable1 = inputs
    .map { first, second in 
        return true // or false depending on the values of first & second.
    }
let outputObservable2 = inputs
    .map { first, second in 
        return true // or false depending on the values of first & second.
    }

Upvotes: 1

Related Questions