Hey buddy this is Caleb with envelopes dot com and and this video we're going to continue talking about
subjects in ARC swift specifically the behavior subject.
Now it's different than the published subject which is what we just talked about and to show you the
differences we're going to go ahead and pull open your ex code playground with our our ex capabilities
and we're going to go ahead and create a new playground page but we're going to copy these top three
lines because we're going to need them anyway so go ahead and copy those.
And right click on publish subject click new playground page and go ahead and name it.
Behavior subject subject.
Excellent.
And select one of the numbered lines press command a and delete everything then paste in our imports
as well as the playground page needs indefinite execution.
Now to make sure that it's running properly you might need to build it and clean it.
And let's just make sure that we have x.
We don't yet.
So it looks like it's still having some trouble here.
So what I'm going to do is I'm going to close out of this and open it again just like so and we'll see
if that will let us use our ax.
Kate still saying no such modules so I'm going to build it and see if that fixes our problem and makes
our error go away.
All right.
So we have now successfully built.
Let's see if when we run the playground if that error goes away.
OK looks like it may have let's double check by try and create an observable of type observable.
There we go.
OK so now we have access.
So just if you have that same problem you can go ahead and close out of your project and reopen it and
then build it again.
And that should solve your problem.
So like I said we're going to be talking about behavior subjects.
OK.
So let's create one var behavior.
Subject equals behavior subject.
Now something you need to know is that a behavior subject needs an initial value.
But before we do that we're going to define behaviors subject now a behavior subject is going to basically
give the subscriber The most recent element and everything that's emitted after that.
So unlike the published subject which only gives you which only gives you elements that are added after
the subscription a behavior subject is going to give you the most recent value as well as everything
that follows.
So I do see that I spelled this wrong so change that behavior subject and in order to create one of
these you need to go ahead and put some parentheses and we're going to pass it a value.
Now this can be a value of anything but we're just going to make it of type string.
And I'm going to call this value a.
OK.
So we now have a behavior subject with an initial value of a string called value.
Now what we can do is we can create a subscription.
Now what we can do is we can create a subscription for our behavior subject and then we can print out
our first value just like we have been.
So go ahead and type let subscription.
I'll just call this subscription one.
And that's going to be equal to behaviour's subject and we need to subscribe to that so that we can
actually use the values that are passed into our observable stream.
So go ahead and type subscribe and we're going to use on next steps with a lower case.
So on next and give it some curly brackets and inside the closure we're going to print out subscription
one and we're going to print out the dollar sign zero value which is our temporary variable as we're
cycling through all of the values.
As you can see value is called When we use this temporary variable and look at that it prints out subscription
1 value.
Pretty cool.
Now if this were a published subject we would not have access to this because a published subject does
not require an initial value and it only pays attention to values that are given after the behavior
or subject has been subscribed.
So let's go ahead let's add a new value here.
We're going to say behavior subject dot on next.
And as you can see we can pass in a new element of type String.
Let's call this value B and let's see what happens.
Subscription one now prints out value A and value B.
Because as I just said it'll give us subscriber of the most recent element as well as all elements that
follow.
Once you subscribe.
So let's add another one and see what happens.
I guess you probably have a prediction of what will happen if I go ahead and give a value see let's
see what happens where you write value c prints out as the third value for the subscription.
So now let's create a new subscription called subscription 2.
And let's see how this changes how our subscriptions are going to print out.
So go ahead and type let subscription to equalize behavior subject to subscribe and we're going to go
ahead and call on next.
And in the closure we're going to print subscription to let's do a number two and we're going to print
out our dollar sign zero value which is going to take anything which is going to take the values from
our on next calls when we pass in value.
Now you might be wondering well what is the deal here why when it prints out are we only getting one
value for subscription.
Two but three values for subscription one.
Now remember a behavior subject takes the most recent value and anything that comes after the subscription.
So in this case the first subscription takes the initial value of A as the most recent value.
Then we add on to new values using on next.
And that's why those show up.
But the most recent value for subscription 2 is value c and that's why we get value c first.
Now if I were to print out another one here behavior's subject on next and I were to pass in a string
like so and say both subscriptions get this value.
Watch what happens.
Subscription 1 and subscription to both print out that string.
Now of course I spelled this wrong subscriptions.
There we go.
But as you can tell a behavior subject gets the initial value or I should say the most recent value
and then anything after the subscription.
It can also access.
So that is what differentiates a behavior subject from a published subject as you can see there a little
bit different and there are different use cases for each one where one might make more sense than another.
So that is one thing that makes behavior subjects unique is that they take an initial value and they
also pay attention to the most recent value and then focus on anything else that comes after the subscription.
So this is behavior subjects we're going to move on next to variables which is a type of observer.
So I'll see in the next video.