Reading Club — Subjects

count number when you received email!!

JoseeWoo
電腦妻日常

--

Our front-end team just launched a reading club of RxSwift last week, the first topic was Observable. This week, it’s my turn to talk about Subject. First time I knew about Reactive Programming I can’t really tell what’s the difference between Observable and Subject until I started working on my new project using MVVM with RxSwift. So I think It’s better that I can start this article by telling the difference between these two. I am not native FRP programmer so I am not going to dig into FRP ( I am still learning it ). There are three points I gonna go through.

First, what’s Subject and the difference between Subject and Observable

Second, the details in different Subjects

Third, where we could use it?

Observable is Passive but Subject is Active!

How? I made a simple illustration. The mailbox is Observable so it can only passively receive incoming mails. But the little girl is different cause she can also receive the mails but also send the mails to her family.(or tell others how many mails she got …). The little girl plays as both Observable and Observer, just like Subject ! She will continue to count the mails she receive even her family doesn’t ask her to do that. Unlike Observable needs to be subscribed to trigger a chain of events, Subject can subscribe to an Observable to receive those signals and send them out (No matter there is subscriber or not). More importantly, Observable can’t emitted signals actively, but subject can.

There are 4 + 1 types of subjects in RxSwift. The marble diagram show more clearly how those subjects work. (After the presentation of reading club, I found AsyncSubject when I looked for some examples online)

AsyncSubject: emits only the last value or error

You can see the first observer subscribes from the beginning, but only receives the last value. The second observer which subscribes later still receives only the last value.

PublishSubject: Sends out the latest elements to subscribers.

PublishSubject

BehaviorSubject: Replays the previous also the latest element to new subscribers.

BehaviorSubject sends out the latest value with the previous value ( if there is no previous value, it sends initial value instead)

ReplaySubject: Initialized with a buffer size and will maintain a buffer of elements up to that size and replay it to new subscribers

ReplaySubject sends out the latest value with the x cached values ( x is set when you initial the ReplaySubject )

Variable: Wraps a BehaviorSubject. It works as BehaviorSubject but it can’t send error events to observers.

Where we could use it ??

After those theoretical introductions of Subjects, I still don’t know how to use them . So I started searching online/ Github/ examples for “best practice” of Subjects. Then…. I realized “There is no best practice for Subject”. Just like Observable, we use Observable/Subject to achieve the architecture, the pattern… we design. It depends on the scenario you set up then you could decide use Subject or not. For me, I used Subject in manager in my new project. Why ? The manager is singleton and I want to store part of my data in the manager and access them later on specific page.

Reasons that I use Subject in my manager

  1. I want my stored data could be shared by different controllers/ view models.
  2. I don’t want to keep triggering same APIs just because of new subscription.
So my manager stores shared status, data and notify observers when the data is updated.

Besides using Subject in the manager, I saw the example on the textbook (see the links in Reference / Resources) it used Subject to notify other controllers when the specific content changes on the current controller. Does it sound familiar ? It replaces the delegation pattern with Subject!

So this is the end, hope you can know a little bit more about Subject!

Welcome to point out / correct my typo ( or leave the comments for more discussions). If you like it, please claps and like it so other people will see this here on Medium.

Reference / Resources

if you want to dig into more …

--

--

JoseeWoo
電腦妻日常

iOS developer @VestiaireCollective. A dreamer needs a lot of alcohol, a drunker has too many dreams.