1
00:00:08,070 --> 00:00:11,170
Hey everybody what's going on this is Caleb with slopestyle.

2
00:00:11,250 --> 00:00:17,670
And in this video we're going to continue talking about subjects more specifically the replay subject.

3
00:00:17,760 --> 00:00:20,420
Now replay subjects are really really cool.

4
00:00:20,430 --> 00:00:27,190
They work very similarly to a behavior subject but you can give them an explicit buffer size.

5
00:00:27,190 --> 00:00:31,120
So let's say that I gave it a buffer size of three.

6
00:00:31,230 --> 00:00:37,740
That means that any future subscribers are going to receive the last three elements that were pushed

7
00:00:37,740 --> 00:00:37,980
on.

8
00:00:37,980 --> 00:00:42,780
So it replays a certain amount of elements to all future subscribers.

9
00:00:42,780 --> 00:00:44,310
It's super duper cool.

10
00:00:44,580 --> 00:00:45,840
So let's get into it now.

11
00:00:45,870 --> 00:00:47,640
I want to show you how it works.

12
00:00:47,640 --> 00:00:54,180
So what we're going to do is we're actually going to create a new playground page by selecting new playground

13
00:00:54,180 --> 00:01:00,840
page like so go ahead and select all and delete all that stuff and you know what we can rename this

14
00:01:00,840 --> 00:01:04,190
here replay subject.

15
00:01:04,260 --> 00:01:09,990
Now of course we're going to need to copy in our imports here as well as our needs indefinite execution

16
00:01:10,650 --> 00:01:12,230
which is kind of a scary sounding line.

17
00:01:12,240 --> 00:01:14,920
But anyway that's what it needs.

18
00:01:15,060 --> 00:01:18,490
So as we've been doing we clean it we build it.

19
00:01:18,730 --> 00:01:23,360
And most of the time we still have trouble with it importing properly.

20
00:01:23,520 --> 00:01:29,030
So we may have to quit out unless it's going to let us use it.

21
00:01:29,630 --> 00:01:35,850
It does not look like it will so I'm going to go ahead and close this stop the task and we're going

22
00:01:35,850 --> 00:01:37,640
to open it one more time.

23
00:01:38,820 --> 00:01:40,180
Build it again.

24
00:01:40,500 --> 00:01:45,420
And we should be able to access all of our arcs swift stuff.

25
00:01:45,420 --> 00:01:46,560
There it is.

26
00:01:46,560 --> 00:01:58,260
OK so we are going to create a replay subject that is going to essentially replay the previous three

27
00:01:58,320 --> 00:01:59,670
bank transactions.

28
00:01:59,750 --> 00:02:00,470
OK.

29
00:02:00,690 --> 00:02:10,770
So we're going to call this var let's say a account subject and we're going to make it of type replay

30
00:02:10,780 --> 00:02:11,590
subject.

31
00:02:11,780 --> 00:02:12,390
OK.

32
00:02:12,660 --> 00:02:18,710
Now we're going to make it of type Let's make it of types not string.

33
00:02:18,710 --> 00:02:25,290
How about double since we're dealing with bank balances and in order to actually create a replaced subject

34
00:02:25,290 --> 00:02:27,730
you need to call DOT create.

35
00:02:27,780 --> 00:02:30,750
And you'll notice that there is a buffer size property.

36
00:02:30,900 --> 00:02:38,640
That's what we want a replay subject has a buffer size and that basically sets the amount of elements

37
00:02:38,640 --> 00:02:40,880
that will be replayed to future subscribers.

38
00:02:40,890 --> 00:02:47,760
Or I guess the maximum amount of elements that can be replayed so let's just do three Caleigh was like

39
00:02:47,760 --> 00:02:49,810
the example in the beginning.

40
00:02:50,130 --> 00:02:52,160
And so let's go ahead and move on.

41
00:02:52,320 --> 00:02:59,230
We have a replay subject that can replay up to three elements for future subscribers.

42
00:02:59,280 --> 00:03:05,450
So let's go ahead and we're going to create what's called an account manager and I'm just calling it

43
00:03:05,490 --> 00:03:12,600
the account manager because it is subscribing to the account subject and it's doing stuff with the items

44
00:03:12,600 --> 00:03:14,190
that are pushed into our subject.

45
00:03:14,190 --> 00:03:21,900
So go ahead and call account manager or var account manager and we're going to go ahead and subscribe

46
00:03:21,990 --> 00:03:23,120
to our account subject.

47
00:03:23,120 --> 00:03:25,850
So go ahead and call account subject.

48
00:03:26,070 --> 00:03:36,420
Subscribe and on next as you may have suspected we're going to go ahead and print out the transaction.

49
00:03:36,420 --> 00:03:36,880
OK.

50
00:03:37,170 --> 00:03:45,690
So go ahead and print and we're going to say transaction amount and we'll use stringing cancellation

51
00:03:45,690 --> 00:03:49,710
here to print out every element as we go through our sequence.

52
00:03:49,710 --> 00:03:55,960
OK the dollar signs zero of course is the temporary variable that's going to house that value.

53
00:03:56,220 --> 00:04:00,200
And this way we can print out every transaction amount that we pass it.

54
00:04:00,210 --> 00:04:03,130
So let's go ahead and let's go see how this works.

55
00:04:03,150 --> 00:04:10,110
If I call account subject on next and I pass it in a double let's say that we had a transaction for

56
00:04:10,110 --> 00:04:12,660
$12 and 49 cents.

57
00:04:12,660 --> 00:04:13,780
Watch what happens.

58
00:04:13,860 --> 00:04:15,960
It prints out saying transaction amount.

59
00:04:15,990 --> 00:04:20,750
Oh you know what we should actually put a dollar sign there so it looks like an actual transaction.

60
00:04:21,650 --> 00:04:27,640
Let's see if that fixed it should have it's just a dollar sign in our print statement.

61
00:04:27,680 --> 00:04:28,140
OK.

62
00:04:28,160 --> 00:04:34,390
Transaction amount 12:49 really cool account subject Dodd on next.

63
00:04:34,390 --> 00:04:43,490
Let's pass in another value maybe we maybe we bought a new Xbox let's say two ninety nine for our next

64
00:04:43,490 --> 00:04:46,660
value transaction amount to ninety nine.

65
00:04:47,270 --> 00:04:48,520
Account subject.

66
00:04:48,520 --> 00:04:50,880
On next let's give it another value.

67
00:04:50,990 --> 00:04:56,120
How about $15 K and it prints out $15.

68
00:04:56,120 --> 00:05:05,720
Now watch what happens when I print out another one account subject on next and let's give it $80 watch

69
00:05:05,720 --> 00:05:08,990
what happens we get a fourth transaction amount.

70
00:05:08,990 --> 00:05:14,020
Now you were probably thinking Wait we're supposed to have a buffer size of three.

71
00:05:14,030 --> 00:05:17,370
It's not supposed to be able to allow me to do more than three.

72
00:05:17,690 --> 00:05:19,840
Well I'll do a fifth one here and we'll see what's going on.

73
00:05:19,880 --> 00:05:20,500
Let's do.

74
00:05:20,510 --> 00:05:23,120
Seventy four dollars and twelve cents.

75
00:05:24,240 --> 00:05:26,350
We get a fifth print.

76
00:05:26,370 --> 00:05:29,150
Now you might be thinking what is going on.

77
00:05:29,160 --> 00:05:34,230
That's because a replay subject actually works just like a behavior subject.

78
00:05:34,230 --> 00:05:37,750
But the first subscription receives everything.

79
00:05:37,800 --> 00:05:43,400
So every on next call that we pass in the first subscription is going to get all of those.

80
00:05:43,410 --> 00:05:47,170
But watch what happens when I create a new subscription.

81
00:05:47,370 --> 00:05:56,670
I'm going to type var last three transactions and we're going to call account subject and we're going

82
00:05:56,670 --> 00:06:05,680
to subscribe on next just like so now what I'm going to do is I'm going to print.

83
00:06:06,210 --> 00:06:06,900
Well you know what.

84
00:06:06,960 --> 00:06:13,190
Let's say let's do the same thing as above transaction amount.

85
00:06:13,750 --> 00:06:14,010
OK.

86
00:06:14,160 --> 00:06:18,050
And we're going to pass in transaction amount zero dollars.

87
00:06:18,090 --> 00:06:20,780
Now the thing that you might be noticing.

88
00:06:21,050 --> 00:06:25,230
Oh you know what I need to make a space between these because it's hard to see so I'm going to print

89
00:06:25,230 --> 00:06:29,400
out a line break there.

90
00:06:30,770 --> 00:06:31,210
OK cool.

91
00:06:31,220 --> 00:06:32,380
So that's easy to see the two.

92
00:06:32,400 --> 00:06:41,450
So I'm also going to add maybe account manager and then I'll add last three just to show you the difference.

93
00:06:41,510 --> 00:06:46,960
So the account manager basically is capturing everything just like a behavior subject.

94
00:06:47,000 --> 00:06:52,640
And the reason I'm saying behavior subject and not publish subject is because I can add elements to

95
00:06:52,640 --> 00:06:56,610
my account subject before I actually subscribe.

96
00:06:56,630 --> 00:06:57,650
And they'll still show up.

97
00:06:57,650 --> 00:07:01,780
Watch this if I passed in ten dollars and 15 cents.

98
00:07:01,970 --> 00:07:06,460
You'll see it will show up in my initial transaction and I did that before I subscribed.

99
00:07:06,560 --> 00:07:09,810
Just like a behavior subject can show the most recent element.

100
00:07:09,850 --> 00:07:12,610
I can also pass in one before I subscribe.

101
00:07:12,650 --> 00:07:16,600
Now the account manager is getting every single thing that I've done.

102
00:07:16,670 --> 00:07:23,840
But look what the last three transactions is capturing $15 80 dollars and 74 12.

103
00:07:23,840 --> 00:07:29,900
Those are the last three elements that have subscribed or that have been pushed into my subscription.

104
00:07:29,900 --> 00:07:32,240
Let's add another one.

105
00:07:32,240 --> 00:07:35,420
How about $100 even.

106
00:07:35,640 --> 00:07:39,990
You'll notice my last three transactions will update so I get the last three.

107
00:07:39,990 --> 00:07:41,410
That's super cool.

108
00:07:41,430 --> 00:07:46,980
So this would be really helpful for kind of like showing a recent history in an app or if you think

109
00:07:46,980 --> 00:07:53,520
about Twitter on the left you'll have like a suggested followers to follow and it shows you three followers.

110
00:07:53,520 --> 00:07:57,810
What I could do is I could have that set up to something where it downloads three followers that are

111
00:07:58,200 --> 00:08:00,030
ones I might like and it'll show.

112
00:08:00,050 --> 00:08:03,250
Or maybe a list of followers and it'll show the most recent three.

113
00:08:03,450 --> 00:08:08,310
And then if I remove one it would show me the next recent three and then so on and so forth that's how

114
00:08:08,880 --> 00:08:11,210
replay subjects can be really helpful.

115
00:08:11,270 --> 00:08:19,230
It's pretty cool as you can see we are basically able to pass in values to our first subscription which

116
00:08:19,230 --> 00:08:21,020
acts like a behavior subject.

117
00:08:21,210 --> 00:08:28,770
We're able to pass in or we receive the last three elements pushed in to our replay subject and that's

118
00:08:28,770 --> 00:08:30,330
all dependent on the buffer size.

119
00:08:30,330 --> 00:08:36,630
Watch what happens if I update it to five when it prints out you'll notice we now have five recent transactions.

120
00:08:36,630 --> 00:08:38,330
OK that's pretty cool.

121
00:08:38,340 --> 00:08:40,130
So this is how replay subjects work.

122
00:08:40,140 --> 00:08:45,240
It's not a very complex idea but it's really cool and really robust there's lots of cool ways that this

123
00:08:45,240 --> 00:08:47,490
could be used in a real application.

124
00:08:47,490 --> 00:08:50,880
So I hope that you've enjoyed learning about replay subjects.

125
00:08:50,880 --> 00:08:55,440
And we're going to move on to the next video where are you going to learn about variables not the normal

126
00:08:55,440 --> 00:08:59,160
swift variables but variables in our ex-wifes they're a little bit different.

127
00:08:59,160 --> 00:09:01,580
Great job with this video and I'll see in the next one.
