Hey everybody this is Caillat with slopes Loeb's dot com and welcome back to the Eric swift course in
this section we're going to talk about subjects now subjects are a special form of observable.
There observables but they have a special function special ability each one is a little bit different
from the other.
Of course just like always you can subscribe and dynamically add elements to subjects just like you
can with observables.
Now there are currently four kinds of subjects in ARC Swift.
We're only going to be talking about three of them but they're pretty cool.
So we're going to talk about publish subjects in this video and then in the next video we'll talk about
behavior subjects and after behavior subjects we're going to talk about variables and you're probably
thinking wait variables that's a normal part of swift.
You're right.
But a variable in our swift is a little bit different.
So we're going to begin by talking about published subjects so go ahead and pull open your artics playground
go ahead and right click on one of the playground pages and click.
New playground page.
We're going to call this publish subject.
And let's go ahead and select all and delete it.
Of course we need to import our acts Swift.
We need to import playground support and we need to call playground page.
Current DOT needs indefinite.
In definite execution and set that to be equal to true.
After that we're going to go ahead and create our very first publish subject.
So go ahead and type var publish subject whip's publish subject and set it to be equal to type published
subject.
Now something you need to know is when you create a published subject you need to give it an explicit
type.
So we're gonna give it an explicit type of string and we're going to instantiate it by using two parentheses.
Now what we can do with a publish subject is we can add values to it.
Check it out.
If I call publish subject make sure that you select the one with the lowercase p.
And if I call dot on next I can pass in an element of string.
So for instance I could say hello and now I have added an element to my published subject.
Now you probably notice why isn't it doing anything.
We haven't subscribed yet so let's go ahead and let's create a subscription.
So go ahead and type let pub sub subscription and we're going to set that to be equal to publish subject
and we're going to call DOT subscribe.
Now we're going to call subscribe on next.
So go ahead and type that.
And then inside of these curly brackets we're going to do something we're going to go ahead and call
print and we're going to use dollar signs 0 to print out whatever we have in our published subject.
And as it runs you'll see it will print out the halo value that we have pushed in with on next.
Now you're probably thinking well wait a minute.
Why is nothing showing up.
Why is nothing printing.
The reason for that is because a published subject works in a very special way a published subject is
going to show you all events but only the ones that occur after you subscribe.
OK so anything that happens before a subscription will not be called so I can do this again.
I could say publish subject on next and I could say world.
You'll notice nothing gets printed but watch what happens after the subscription.
If I type publish subject on next and I can say hello and as you can see it prints out hello because
we added this using on next after we subscribed.
Now I could do it again publish subject on next and I can say again hello again.
So as you can see it prints out hello and again but it's ignoring everything that happens before we
subscribed.
So that is what makes a published subject unique.
If you subscribe to it you will get all the events that are going to happen after the subscription.
Now I want to create a new subscription here to show you what happens when you're dealing with multiple
subscriptions.
So go ahead and call Let pub sub subscription too and we're going to make a new subscription here.
So using published subject let's say we want to use the same subject we can call DOT subscribe and we
can call on next.
Now using curly brackets I can go in here and inside the subscribe on next closure I can do the same
thing.
I can print dollar signs zero.
Now you'll notice nothing is going to print for pub sub.
But what I can do is call publish subject on next and I can say both subscriptions get this message
and watch what happens.
Both subscriptions printed because this happened after both subscriptions what happened before did not
get to the first one.
What happened after did not get to the second one because these were all before the second subscription.
But if I call on next after both both subscriptions get the same message so there are lots of really
cool ways that publish subjects can be used.
It shows everything after a certain point of subscription.
Pretty neat.
So this is a brief overview of published subjects.
It's a specific kind of observer where you can add in values you can make subscriptions.
But the only values that will actually be noticed and used when subscribing are ones that occur after
the subscription asynchronously.
So this is published subjects in the next video we're going to talk about behavior subjects which are
similar but quite different.
So I'll see in the next video.
Awesome job with this one guys.