The answer is
theObservable
.subscribe(onNext: { theSubject.onNext($0) })
.disposed(by: disposeBag)
This will make sure that every time that the theObservable
emits, the value will be passed to theSubject
too.
Note
This only passes the value onNext
, if you want to handle all the cases, then use bind(to:)
as the answer by Daniel T. (or drive
for Driver
s)
Example with more Observables
In the following example values from different Observables
will be passed to theSubject
let theSubject = PublishSubject<String>()
let theObservable = Observable.just("Hello?")
let anotherObservable = Observable.just("Hey there")
theSubject.asObservable()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
theObservable
.subscribe(onNext: { theSubject.onNext($0) })
.disposed(by: disposeBag)
anotherObservable
.subscribe(onNext: { theSubject.onNext($0) })
.disposed(by: disposeBag)
Output
