Hey everybody what's going on this is Caleb with slopestyle.
And in this video we're going to continue talking about subjects more specifically the replay subject.
Now replay subjects are really really cool.
They work very similarly to a behavior subject but you can give them an explicit buffer size.
So let's say that I gave it a buffer size of three.
That means that any future subscribers are going to receive the last three elements that were pushed
on.
So it replays a certain amount of elements to all future subscribers.
It's super duper cool.
So let's get into it now.
I want to show you how it works.
So what we're going to do is we're actually going to create a new playground page by selecting new playground
page like so go ahead and select all and delete all that stuff and you know what we can rename this
here replay subject.
Now of course we're going to need to copy in our imports here as well as our needs indefinite execution
which is kind of a scary sounding line.
But anyway that's what it needs.
So as we've been doing we clean it we build it.
And most of the time we still have trouble with it importing properly.
So we may have to quit out unless it's going to let us use it.
It does not look like it will so I'm going to go ahead and close this stop the task and we're going
to open it one more time.
Build it again.
And we should be able to access all of our arcs swift stuff.
There it is.
OK so we are going to create a replay subject that is going to essentially replay the previous three
bank transactions.
OK.
So we're going to call this var let's say a account subject and we're going to make it of type replay
subject.
OK.
Now we're going to make it of type Let's make it of types not string.
How about double since we're dealing with bank balances and in order to actually create a replaced subject
you need to call DOT create.
And you'll notice that there is a buffer size property.
That's what we want a replay subject has a buffer size and that basically sets the amount of elements
that will be replayed to future subscribers.
Or I guess the maximum amount of elements that can be replayed so let's just do three Caleigh was like
the example in the beginning.
And so let's go ahead and move on.
We have a replay subject that can replay up to three elements for future subscribers.
So let's go ahead and we're going to create what's called an account manager and I'm just calling it
the account manager because it is subscribing to the account subject and it's doing stuff with the items
that are pushed into our subject.
So go ahead and call account manager or var account manager and we're going to go ahead and subscribe
to our account subject.
So go ahead and call account subject.
Subscribe and on next as you may have suspected we're going to go ahead and print out the transaction.
OK.
So go ahead and print and we're going to say transaction amount and we'll use stringing cancellation
here to print out every element as we go through our sequence.
OK the dollar signs zero of course is the temporary variable that's going to house that value.
And this way we can print out every transaction amount that we pass it.
So let's go ahead and let's go see how this works.
If I call account subject on next and I pass it in a double let's say that we had a transaction for
$12 and 49 cents.
Watch what happens.
It prints out saying transaction amount.
Oh you know what we should actually put a dollar sign there so it looks like an actual transaction.
Let's see if that fixed it should have it's just a dollar sign in our print statement.
OK.
Transaction amount 12:49 really cool account subject Dodd on next.
Let's pass in another value maybe we maybe we bought a new Xbox let's say two ninety nine for our next
value transaction amount to ninety nine.
Account subject.
On next let's give it another value.
How about $15 K and it prints out $15.
Now watch what happens when I print out another one account subject on next and let's give it $80 watch
what happens we get a fourth transaction amount.
Now you were probably thinking Wait we're supposed to have a buffer size of three.
It's not supposed to be able to allow me to do more than three.
Well I'll do a fifth one here and we'll see what's going on.
Let's do.
Seventy four dollars and twelve cents.
We get a fifth print.
Now you might be thinking what is going on.
That's because a replay subject actually works just like a behavior subject.
But the first subscription receives everything.
So every on next call that we pass in the first subscription is going to get all of those.
But watch what happens when I create a new subscription.
I'm going to type var last three transactions and we're going to call account subject and we're going
to subscribe on next just like so now what I'm going to do is I'm going to print.
Well you know what.
Let's say let's do the same thing as above transaction amount.
OK.
And we're going to pass in transaction amount zero dollars.
Now the thing that you might be noticing.
Oh you know what I need to make a space between these because it's hard to see so I'm going to print
out a line break there.
OK cool.
So that's easy to see the two.
So I'm also going to add maybe account manager and then I'll add last three just to show you the difference.
So the account manager basically is capturing everything just like a behavior subject.
And the reason I'm saying behavior subject and not publish subject is because I can add elements to
my account subject before I actually subscribe.
And they'll still show up.
Watch this if I passed in ten dollars and 15 cents.
You'll see it will show up in my initial transaction and I did that before I subscribed.
Just like a behavior subject can show the most recent element.
I can also pass in one before I subscribe.
Now the account manager is getting every single thing that I've done.
But look what the last three transactions is capturing $15 80 dollars and 74 12.
Those are the last three elements that have subscribed or that have been pushed into my subscription.
Let's add another one.
How about $100 even.
You'll notice my last three transactions will update so I get the last three.
That's super cool.
So this would be really helpful for kind of like showing a recent history in an app or if you think
about Twitter on the left you'll have like a suggested followers to follow and it shows you three followers.
What I could do is I could have that set up to something where it downloads three followers that are
ones I might like and it'll show.
Or maybe a list of followers and it'll show the most recent three.
And then if I remove one it would show me the next recent three and then so on and so forth that's how
replay subjects can be really helpful.
It's pretty cool as you can see we are basically able to pass in values to our first subscription which
acts like a behavior subject.
We're able to pass in or we receive the last three elements pushed in to our replay subject and that's
all dependent on the buffer size.
Watch what happens if I update it to five when it prints out you'll notice we now have five recent transactions.
OK that's pretty cool.
So this is how replay subjects work.
It's not a very complex idea but it's really cool and really robust there's lots of cool ways that this
could be used in a real application.
So I hope that you've enjoyed learning about replay subjects.
And we're going to move on to the next video where are you going to learn about variables not the normal
swift variables but variables in our ex-wifes they're a little bit different.
Great job with this video and I'll see in the next one.