Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In RxSwift how can I set up a Subject to observe another Observable?

Let's say I have the following:

let theSubject = PublishSubject<String>()
let theObservable = Observable.just("Hello?")

How do I set the theSubject to observer theObservable?

In RxSwift we say that a subject is an observer and can subscribe to one or more Observables...

Can you show a simple code example of how I can connect theSubject as an observer of theObservable ?

like image 818
Andrew Paul Simmons Avatar asked Aug 23 '18 12:08

Andrew Paul Simmons


People also ask

What is observer in RxSwift?

The heart of the RxSwift framework is based on observable which is also known as a sequence. Observable is the sequence of data or events which can be subscribed and can be extended by applying different Rx operators like map, filter, flatMap, etc. It can receive data asynchronously.

What are subjects in RxSwift?

Subjects act as both an observable and an observer. They can receive events and also be subscribed to. The subject received . next events, and each time it received an event, it turned around and emitted it to its subscriber.

What is PublishRelay?

PublishRelay. This type of Relay will reemit all events once the Observer has subscribed to it.

What is single RxSwift?

A Single is something like an Observable that instead of emitting a series of values, is guaranteed to be return either a value or an error. Emits exactly one element, or an error. Doesn't share side effects.


2 Answers

The code is:

theObservable
    .bind(to: theSubject)
    .disposed(by: bag)

or:

theObservable 
    .subscribe(theSubject)
    .disposed(by: bag)

If you only do subscribe(onNext:) as others have suggested, only the onNext events will get passed along. You should use bind to pass everything along.

(But really you probably shouldn't use a subject. Instead bind the thing(s) that are listening to the subject to the Observable directly.

like image 123
Daniel T. Avatar answered Nov 14 '22 23:11

Daniel T.


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 Drivers)

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

The subject

like image 8
E-Riddie Avatar answered Nov 14 '22 21:11

E-Riddie