RxSwift For Dummies š¤ Part 3
Letās learn about the next building block of RxSwift: Subjects
I think we can agree that when you have a reference to an Observable
itās output only. You can subscribe to itās output, but you canāt change it.
A Subject
is an output(Observable
) but itās also an input! That means that you can dynamically/imperatively emit new elements in a sequence.
onNext
is the method you use to do the input.
It will of course print:
Why do we need subjects? To easily connect the declarative/RxSwift world with the imperative/normal world. Subjects feel more ānaturalā to programmers used to imperative programming.
In a perfect, clean RxSwift implementation you are discouraged to use subjects - you should have a perfect stream of observables. Letās not bother our heads with it though, Iāll explain that in a separate post, please use as much subjects as you want for now š
So thatās basically it! Letās learn to control subjects now.
Hotš„ vs Coldāļø
I mentioned the Hotš„ vs Coldāļø in the first part of the tutorial. Letās get into it in more detail now, because subjects are actually the first hot observables weāre encountering.
As we already established, when you create/declare an Observable
using create
it wonāt execute until thereās an observer that observes on it. Itāll start execution at the same moment something calls subscribe
on it. Thatās why itās called a coldāļø observable. If you donāt remember you can take a quick look at Part 1
A hotš„ observable will emit itās elements even it if has no observers. And thatās exactly what subjects do.
Pretty straightforward, huh? If you understood the cold observable in Part 1, hot observable should give you no problems as itās more intuitive/natural.
Subject Types
There are three commonly used subject types. They all behave almost the same with one difference: each one does something different with values emitted before the subscription happened.
ā¢ Publish Subject
As you could see in the experiment above the publish subject will ignore all elements that were emitted before subscribe
have happened.
You use it when youāre just interested in future values.
ā¢ Replay Subject
Replay subject will repeat last N number of values, even the ones before the subscription happened. The N is the buffer, so for our example itās 3
:
You use it when youāre interested in all values of the subjects lifetime.
ā¢ Behavior Subject
Behavior subject will repeat only the one last value. Moreover itās initiated with a starting value, unlike the other subjects.
You use it when you just need to know the last value, for example the array of elements for your table view.
Binding
You can bind an Observable
to a Subject
. It means that the Observable
will pass all itās values in the sequence to the Subject
Thereās sugar syntax to simplify it a little bit called bindTo
:
It will of course print I'm being passed around š²
.
Warning
Binding will pass not only values, but also completed
and error
events on which case the Subject
will get disposed and wonāt react to any more events - it will get killed.
Quick Example
Letās modify the example from the first post a little.
As you can see we have a view model that exposes a googleString
subject that view controllers can subscribe to. We bind the observable to the subject, so when a reply from the server comes itāll emit itās value and it will get passed in the subject.
Bonus: Variable
Thereās one more thing missing if you want to totally violate the declarative nature of RxSwift: reading the last emitted value imperatively.
Thatās where Variable
comes in. Variable
is just a simple wrapper over BehaviorSubject
. Feel free to take a look yourself - the implementation is really easy. Itās very handy.
Letās say for example that we want to be able to access the ācurrentā googleString
at any time.
Youāll learn to love it. Itās like RxSwift in easy mode āŗļø
Seems easy, but thereās a lot of traps to watch out for, so please refer to my next post: RxSwift Safety Manual