What Are Subjects In RxSwift?
Observers and Observables RxSwift
Before starting this topic I assume you have a basic knowledge of RxSwift or you have red my previous article “Introduction To RxSwift”. If you don’t have basic knowledge of RxSwift I insist you to read my previous article.

Subjects:
A very simple and easy definition of subjects is they are “observer” & “observable” . Let me explain you this in quick and easy way with an image.

In the image above we can clearly see the behavior of the a subject. It is observing from the observable and passing it to the subscriber. For the observable the subject is acting as observer where as for the subscriber subject is acting as an observable.
Types of subjects :
- Publish Subjects
- Behavior Subjects
- Replay Subjects
- BehaviorRelay

Publish Subjects :
- Doesn’t require an initial value
- The events passed before subscription will be ignored
- Since they act as observable to subscriber once “Completed” or “Error” emits they will ignore all the remaining events and stop emiting

As you can see event passed on line 53 is completely ignored and the printed event is only “2” since we passed it after subscribing.
Have a look at this image for better understanding:

The image above shows that when we don’t have any subscriber for the subject the subject is not working as observable hence the passed value before subscribing was ignored.
Behavior Subject:
- Requieres initial value
- emits only the last value which was given before subscribing
Lets have a look to some practical implementation for better understanding

The image shows us that the event printed was the initial value of behaviorSubject so just don’t assume it will always print the initial value given to it before subscribing. Lets have a look to another image passing another value apart from the initial value before subscribing.

Now this time the second initial value was printed because as I mention earlier it will emit the last value passed to it before subscribing. Now lets see what happens when we pass another value after subscribing.

So here we printed the last value we passed before subscribing and then the value we passed after subscribing. This is the only difference between Publish Subject and Behavior Subject thats why behavior subject requires an initial value.
Replay Subject:
- Replays the events based on the buffer size that you passed.
- If you have passed buffer size 2 and there are total 5 events it will only play the last two events from the 5 events and also the buffer size will be applied to the events that are passed before subscribing.
Lets have a look at practical implementation of this.

So in the image above I passed the buffer size of 2 and number of events passed was 4 as you can see it only replayed the last 2 out of 4 now lets see what happens when we pass events after subscribing.

So it printed the last two events that was passed before subscribing and then all the remaining events that was passed after subscribing.
Behavior Relay:
- Requires initial value
- Value cannot be changed as it is read only. But it can accept new value.
- Requires to import RxCocoa
Lets have a look at practical implementation :

As you can see the last printed value is the initial value

The relay will only accept new value so it means only one event at a time. Alright what if we have an array can we append items to it? The answer is a big NO! but the good news is there is a work around. We will store the value of relay in a variable and append to that variable once we are done appending to the variable we can pass that variable to the relay. Lets have a look at practical implementation.

As you can see we have successfully appended the new values.
Hope after reading this article you got a brief understanding of Subjects.