What Are Subjects In 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.

RxSwift subjects

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.

RxSwift subject

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
RxSwift subjects

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
RxSwift subjects

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:

Observer in RxSwift subjects

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

RxSwift subjects

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.

RxSwift subjects

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.

RxSwift subjects

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.

RxSwift Observer and Observable

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 :

RxSwift Observer and Observable

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

RxSwift Observer and Observable

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.

RxSwift Observer and Observable Subjects

As you can see we have successfully appended the new values.

Hope after reading this article you got a brief understanding of Subjects.

--

--

--

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

API Authentication with Phoenix and React — part 1

How Long Does It Take To Develop An App?

How to check for NaN in JavaScript

Top 10 Time-Saving Gatsby Plugins🔌

JavaScript Key Points-1

Demonstrating Recursion through call stack game!

Setting up CKEditor with Angular 10

How To Apply SOLID Principles To Clean Your Code in React

Clean desk setup

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Umair Ishrat Khan

Umair Ishrat Khan

An energetic and motivated individual IOS developer/ Data Science Practitioner. Apart from computer science Martial arts interests me the most.

More from Medium

Lifecycle of a UIViewController:

Creating your own CocoaPod library

Tableview in UITableviewcell

How to start learning XCUITest from scratch