Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the differences between observable and subject in rxSwift

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?

like image 550
Dean Lee Avatar asked Jul 17 '17 09:07

Dean Lee


People also ask

What is the difference between Observable and 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.

What are subjects in RxSwift?

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.

What are Observables in RxSwift?

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.

What is subject and Observable in angular?

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.


3 Answers

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.

like image 136
Ahmad F Avatar answered Oct 10 '22 17:10

Ahmad F


I think and as per I learned about this both topics, i can say that,

Observables

  • An Observable(fundamental part of Rx) is sequence with some special features. and most important feature is asynchronous. Observables produce some events(i.e onNext, onError, onCompleted), which called as emitting. Events contains some value(i.e Int, Bool, Array or custom type).

Subjects

  • Simple observable can only emits events, which can be subscribed. but what if we want to add some value on current observable(also called self observer). So simply i can say that something that works as an observable and also as a observer is called subjects.
like image 30
Jaydeep Vora Avatar answered Oct 10 '22 17:10

Jaydeep Vora


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.

like image 39
Daniel T. Avatar answered Oct 10 '22 16:10

Daniel T.