What are the differences between observable and subject. When I define a observable type variable. It can emit onNext,onComplete,onDispose. However subject can do the same. When should I use observable and in what case should I use subject?
While plain Observables are unicast (each subscribed Observer owns an independent execution of the Observable), Subjects are multicast. A Subject is like an Observable, but can multicast to many Observers. Subjects are like EventEmitters: they maintain a registry of many listeners. Every Subject is an Observable.
There are four subject types in RxSwift: PublishSubject : Starts empty and only emits new elements to subscribers. BehaviorSubject : Starts with an initial value and replays it or the latest element to new subscribers.
An observable emits next events that contain elements. It can continue to do this until a terminating event is emitted, i.e., an error or completed event. Once an observable is terminated, it can no longer emit events.
A Subject is a special type of Observable that allows values to be multicasted to many Observers. The subjects are also observers because they can subscribe to another observable and get value from it, which it will multicast to all of its subscribers. Basically, a subject can act as both observable & an observer.
In order to understand the difference between them, we should mention that Observable is:
In ReactiveX an observer subscribes to an Observable. Then that observer reacts to whatever item or sequence of items the Observable emits. This pattern facilitates concurrent operations because it does not need to block while waiting for the Observable to emit objects, but instead it creates a sentry in the form of an observer that stands ready to react appropriately at whatever future time the Observable does so.
In other words, observable is data producer (responsible for posting notifications to be observed).
Actually, Subject is a special type of Observables (you still can subscribe to messages like any other observable):
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 reemitting them, and it can also emit new items.
but the thing is subject is a representation -as mentioned in the documentation- of both observable and observer, which means that subject might be data producer (responsible for posting notifications to be observed or data consumer (responsible for receiving notifications).
Also: For checking the types of the subjects, you might want to check: RxSwift Subject Types.
I think and as per I learned about this both topics, i can say that,
Observables
Subjects
You got a couple of answers explaining the difference between Observables and Subjects, but nobody has covered your second question...
When should I use observable and in what case should I use subject?
Here is an excellent, if complex, answer to that question: http://davesexton.com/blog/post/To-Use-Subject-Or-Not-To-Use-Subject.aspx
The TL;DR is this. Use an Observable whenever possible, use a Subject whenever necessary.
You use a Subject whenever you need a hot observable and don't already have an observable to work with. RxCocoa, for example, uses Subjects extensively to create observables for you that are tied to particular UI elements. They are primarlly for bridging non-Rx code into Rx code and connecting producers to consumers where the latter must be created first for some reason.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With