Subject

Subject is a special type in RxSwift that can act as both of these:

  • An Observable sequence, which means it can be subscribed to
  • An Observer that enables adding new elements onto a subject that will then be emitted to the subject subscribers

There are four subject types in RxSwift, each with unique characteristics that you may find useful in different scenarios. They are as listed:

  • PublishSubject
  • BehaviorSubject
  • ReplaySubject
  • Variable

Let's discuss each one of these subjects in turn.

PublishSubject emits only new next events to its subscribers; in other words, elements added to a PublishSubject before a subscriber subscribes will not be received by that subscriber. This concept of emitting previous next events to new subscribers is called replaying, and publish subjects DO NOT replay. If terminated, though, a publish subject will re-emit its stop event, that is, a completed or error event to its new subscribers; actually, all subjects will re-emit stop events to new subscribers. If you are creating a real-time indicator, you won't care what the last element was before a new subscription, you only want new elements coming in after the fact. PublishSubject will be useful in that scenario.

Sometimes you want new subscribers to always receive the most recent next event in the sequence even if they subscribed after that event was emitted; for this, you can use a BehaviorSubject. A BehaviorSubject is initialized with a starting value, and then it replays to the new subscribers a next event containing the most recent elements or the initial value if no new recent elements have been added to it beforehand. Remember that if any subject type is terminated, it will reemit its stop event, whether that is a completed or a new event to its subscribers. For example, in a chat app, you might use a BehaviorSubject to prefill a new posts tile text field beginning with the initial name untitled. As you will see later, you can then bind that text field to a submit button that is only enabled if the text field has text.

If you want to replay more than the most recent element on a sequence to new subscribers, you use a ReplaySubject. A ReplaySubject is initialized with a buffer size and that value cannot be changed after initialization. It will maintain a buffer up to the buffer size of the most recent next events, and it will replay the buffer to the new subscribers as if those events had happened immediately after each other, right after subscribing. It will also reemit its stop event to new subscribers. You can use replay subject to display as many as the five most recent search items whenever a search controller is presented.

A variable is essentially a wrapper around BehaviorSubject. It will replay a next event with the latest or initial value to new subscribers. A variable is guaranteed to never emit an error event and terminate. It also automatically completes when its about to be deallocated, which makes this subject unique. Unlike other subject types, a variable uses the dot "." syntax to get the latest value or to set a new value onto it. You can access a variable's BehaviorSubject by calling as Observable() on it.

We will cover all these subjects in code in the coming sections, so do not worry if your current understanding is not yet crystal clear. This section served to give you a brief introduction about what's to come in the next sections.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset