RxSwift — Reactive Thinking (Part 2)

In Part 1, we have covered the Observable and event emitted by the Observables. Here, will cover a special type of Observables which emits an event on every update of the Observable object. i.e. insertion of an element, deletion of an element or any modification in the element.
A Subject is a special type of observable which doesn’t call the onCompleted() until it is unsubscribed/deregistered. (Explicitly call dispose() method or deinit of DisposeBag is called.)
There is 4 type of subject.
1. ) PublishSubject
Subscriber of this Subject will only receive event emitted after a subscription.
Output: - After Subscribe
2. ) BehaviorSubject
A subscriber of this Subject will receive the last event emitted before subscription and all the event emitted after the subscription.
Output:-
InitialValueUpdatedValueDisposed
In the above example, we have tried to demonstrate how to unsubscribe explicitly by calling dispose(). We haven’t used the DisposeBag.
3. ) ReplaySubject
As name states, it allows us to replay the earlier events and will be received on the initial subscription. We can decide, how many previous events has to be replayed/cached/stacked.
Output:-
world!!!you will not recieve hello :)
4. ) Variable [DEPRECATED]
`Variable` is planned for future deprecation. Please consider `BehaviorRelay` as a replacement. Read more at: https://git.io/vNqvx
The characteristics of the Variable are similar to the BehaviourSubject. It allows us to modify the value of this directly similar to what we do with the normal variable.
Event will be fired twice. On assignment & append.
Output:-
AntonJhonAntonJhonNeil
5. ) BehaviourRelay [As a replacement for Variable]
Relay that emits the most recent item it has observed and all subsequent observed items to each subscribed Observer
.
Output:-
Omg!!!Let's send new data
BehaviorRelay
lies in RxCocoa, I don’t know why? so an import RxCocoa
is needed.
Keep reading Trait in part 3.
RxSwift — Reactive Thinking (Part 3)
