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.
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
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.
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
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.
You can bind an
Observable to a
Subject. It means that the
Observable will pass all it’s values in the sequence to the
There’s sugar syntax to simplify it a little bit called
It will of course print
I'm being passed around 😲.
Binding will pass not only values, but also
error events on which case the
Subject will get disposed and won’t react to any more events - it will get killed.
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.
There’s one more thing missing if you want to totally violate the declarative nature of RxSwift: reading the last emitted value imperatively.
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
A passionate iOS dev always trying to get to the bottom of stuff.