18

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?

Dean Lee
  • 781
  • 2
  • 7
  • 13

3 Answers3

14

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • The Swift Pearls link is broken. Seems the URL has been taken over and can potentially direct to phishing sites. – Ian Rahman Nov 17 '21 at 23:20
4

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.
Jaydeep Vora
  • 6,085
  • 1
  • 22
  • 40
4

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.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72