RxJava - Subjects

 

What is Subject?

A Subject is a sort of bridge or proxy that is available in some implementations of ReactiveX that acts both as an observer and as an Observable. Because it is an observer, it can subscribe to one or more Observables, and because it is an Observable, it can pass through the items it observes by re-emitting them, and it can also emit new items

 

The best way to use Subjects when we have an unknown number of sources and a single Observable. Subjects can be helpful for decoupling between Observable and Observers.

 

There are below type of subjects available

  • PublishSubject
  • BehaviorSubject
  • ReplaySubject
  • AsyncSubject
  • UnicastSubject

Publish Subject

Emits only those items which are emitted after time of subscription.

Behavior Subject

Upon subscription, emits the most recent item then continue to emit item emitted by the source Observable.

Replay Subject

Emits all the items emitted by source Observable regardless of when it has subscribed the Observable.

Async Subject

Emits the last item emitted by the source Observable after it's completes emission.

UnicastSubject

The last implementation is interesting and might be useful. It is similar to other Subjects. It is used to observe and subscribe to the sources

*

RxJava Tutorial RxJava - Environment Setup RxJava’s Characteristics RxJava - How Observable works RxJava - Single Observable RxJava - MayBe Observable RxJava - Completable Observable RxJava - Using CompositeDisposable RxJava - Creating Operators RxJava - Transforming Operators RxJava - Filtering Operators RxJava - Combining Operators RxJava - Utility Operators RxJava - Conditional Operators RxJava - Mathematical Operators RxJava - Subjects RxJava - PublishSubject RxJava - BehaviorSubject RxJava - AsyncSubject RxJava - ReplaySubject RxJava - Schedulers RxJava - Trampoline Scheduler RxJava - NewThread Scheduler RxJava - Computation Scheduler RxJava - IO Scheduler RxJava - From Scheduler RxJava - Buffering