1
00:00:08,100 --> 00:00:13,530
Hey buddy this is Caleb with envelopes dot com and and this video we're going to continue talking about

2
00:00:13,530 --> 00:00:18,090
subjects in ARC swift specifically the behavior subject.

3
00:00:18,090 --> 00:00:22,560
Now it's different than the published subject which is what we just talked about and to show you the

4
00:00:22,560 --> 00:00:30,030
differences we're going to go ahead and pull open your ex code playground with our our ex capabilities

5
00:00:30,540 --> 00:00:35,040
and we're going to go ahead and create a new playground page but we're going to copy these top three

6
00:00:35,040 --> 00:00:38,740
lines because we're going to need them anyway so go ahead and copy those.

7
00:00:38,760 --> 00:00:44,120
And right click on publish subject click new playground page and go ahead and name it.

8
00:00:44,120 --> 00:00:48,730
Behavior subject subject.

9
00:00:48,810 --> 00:00:49,510
Excellent.

10
00:00:49,620 --> 00:00:57,510
And select one of the numbered lines press command a and delete everything then paste in our imports

11
00:00:57,570 --> 00:01:01,980
as well as the playground page needs indefinite execution.

12
00:01:01,980 --> 00:01:06,810
Now to make sure that it's running properly you might need to build it and clean it.

13
00:01:06,810 --> 00:01:09,250
And let's just make sure that we have x.

14
00:01:10,350 --> 00:01:11,620
We don't yet.

15
00:01:11,640 --> 00:01:14,690
So it looks like it's still having some trouble here.

16
00:01:14,700 --> 00:01:23,130
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

17
00:01:23,130 --> 00:01:26,470
if that will let us use our ax.

18
00:01:26,500 --> 00:01:33,120
Kate still saying no such modules so I'm going to build it and see if that fixes our problem and makes

19
00:01:33,120 --> 00:01:34,290
our error go away.

20
00:01:37,830 --> 00:01:38,120
All right.

21
00:01:38,130 --> 00:01:40,600
So we have now successfully built.

22
00:01:40,710 --> 00:01:43,450
Let's see if when we run the playground if that error goes away.

23
00:01:45,730 --> 00:01:52,970
OK looks like it may have let's double check by try and create an observable of type observable.

24
00:01:53,020 --> 00:01:53,530
There we go.

25
00:01:53,540 --> 00:01:55,080
OK so now we have access.

26
00:01:55,120 --> 00:02:00,490
So just if you have that same problem you can go ahead and close out of your project and reopen it and

27
00:02:00,490 --> 00:02:01,320
then build it again.

28
00:02:01,330 --> 00:02:03,060
And that should solve your problem.

29
00:02:03,070 --> 00:02:07,100
So like I said we're going to be talking about behavior subjects.

30
00:02:07,150 --> 00:02:07,450
OK.

31
00:02:07,480 --> 00:02:11,550
So let's create one var behavior.

32
00:02:12,640 --> 00:02:16,840
Subject equals behavior subject.

33
00:02:16,840 --> 00:02:21,830
Now something you need to know is that a behavior subject needs an initial value.

34
00:02:22,060 --> 00:02:28,930
But before we do that we're going to define behaviors subject now a behavior subject is going to basically

35
00:02:28,990 --> 00:02:34,990
give the subscriber The most recent element and everything that's emitted after that.

36
00:02:34,990 --> 00:02:43,480
So unlike the published subject which only gives you which only gives you elements that are added after

37
00:02:43,480 --> 00:02:50,440
the subscription a behavior subject is going to give you the most recent value as well as everything

38
00:02:50,440 --> 00:02:51,240
that follows.

39
00:02:51,310 --> 00:02:57,580
So I do see that I spelled this wrong so change that behavior subject and in order to create one of

40
00:02:57,580 --> 00:03:02,040
these you need to go ahead and put some parentheses and we're going to pass it a value.

41
00:03:02,060 --> 00:03:06,540
Now this can be a value of anything but we're just going to make it of type string.

42
00:03:06,640 --> 00:03:09,250
And I'm going to call this value a.

43
00:03:09,360 --> 00:03:09,750
OK.

44
00:03:09,760 --> 00:03:15,790
So we now have a behavior subject with an initial value of a string called value.

45
00:03:16,150 --> 00:03:20,080
Now what we can do is we can create a subscription.

46
00:03:20,110 --> 00:03:25,450
Now what we can do is we can create a subscription for our behavior subject and then we can print out

47
00:03:25,810 --> 00:03:27,550
our first value just like we have been.

48
00:03:27,550 --> 00:03:31,430
So go ahead and type let subscription.

49
00:03:31,450 --> 00:03:33,300
I'll just call this subscription one.

50
00:03:33,760 --> 00:03:39,910
And that's going to be equal to behaviour's subject and we need to subscribe to that so that we can

51
00:03:39,910 --> 00:03:44,050
actually use the values that are passed into our observable stream.

52
00:03:44,080 --> 00:03:49,780
So go ahead and type subscribe and we're going to use on next steps with a lower case.

53
00:03:49,790 --> 00:03:59,830
So on next and give it some curly brackets and inside the closure we're going to print out subscription

54
00:04:00,430 --> 00:04:06,880
one and we're going to print out the dollar sign zero value which is our temporary variable as we're

55
00:04:06,880 --> 00:04:09,140
cycling through all of the values.

56
00:04:09,160 --> 00:04:15,250
As you can see value is called When we use this temporary variable and look at that it prints out subscription

57
00:04:15,250 --> 00:04:16,530
1 value.

58
00:04:16,840 --> 00:04:17,910
Pretty cool.

59
00:04:17,920 --> 00:04:23,200
Now if this were a published subject we would not have access to this because a published subject does

60
00:04:23,200 --> 00:04:30,870
not require an initial value and it only pays attention to values that are given after the behavior

61
00:04:30,870 --> 00:04:32,580
or subject has been subscribed.

62
00:04:32,680 --> 00:04:35,250
So let's go ahead let's add a new value here.

63
00:04:35,260 --> 00:04:38,670
We're going to say behavior subject dot on next.

64
00:04:38,680 --> 00:04:42,660
And as you can see we can pass in a new element of type String.

65
00:04:42,760 --> 00:04:46,900
Let's call this value B and let's see what happens.

66
00:04:47,290 --> 00:04:51,740
Subscription one now prints out value A and value B.

67
00:04:51,940 --> 00:04:58,780
Because as I just said it'll give us subscriber of the most recent element as well as all elements that

68
00:04:58,780 --> 00:04:59,680
follow.

69
00:04:59,710 --> 00:05:00,960
Once you subscribe.

70
00:05:01,210 --> 00:05:03,560
So let's add another one and see what happens.

71
00:05:03,580 --> 00:05:11,840
I guess you probably have a prediction of what will happen if I go ahead and give a value see let's

72
00:05:11,840 --> 00:05:17,920
see what happens where you write value c prints out as the third value for the subscription.

73
00:05:18,260 --> 00:05:22,400
So now let's create a new subscription called subscription 2.

74
00:05:22,580 --> 00:05:26,000
And let's see how this changes how our subscriptions are going to print out.

75
00:05:26,000 --> 00:05:35,840
So go ahead and type let subscription to equalize behavior subject to subscribe and we're going to go

76
00:05:35,840 --> 00:05:38,040
ahead and call on next.

77
00:05:38,480 --> 00:05:47,510
And in the closure we're going to print subscription to let's do a number two and we're going to print

78
00:05:47,510 --> 00:05:55,310
out our dollar sign zero value which is going to take anything which is going to take the values from

79
00:05:55,430 --> 00:05:57,840
our on next calls when we pass in value.

80
00:05:57,860 --> 00:06:03,740
Now you might be wondering well what is the deal here why when it prints out are we only getting one

81
00:06:03,740 --> 00:06:05,090
value for subscription.

82
00:06:05,090 --> 00:06:08,670
Two but three values for subscription one.

83
00:06:09,020 --> 00:06:16,190
Now remember a behavior subject takes the most recent value and anything that comes after the subscription.

84
00:06:16,400 --> 00:06:22,400
So in this case the first subscription takes the initial value of A as the most recent value.

85
00:06:22,490 --> 00:06:25,340
Then we add on to new values using on next.

86
00:06:25,340 --> 00:06:26,950
And that's why those show up.

87
00:06:26,960 --> 00:06:34,760
But the most recent value for subscription 2 is value c and that's why we get value c first.

88
00:06:34,760 --> 00:06:41,390
Now if I were to print out another one here behavior's subject on next and I were to pass in a string

89
00:06:41,660 --> 00:06:47,770
like so and say both subscriptions get this value.

90
00:06:47,780 --> 00:06:49,100
Watch what happens.

91
00:06:50,260 --> 00:06:55,230
Subscription 1 and subscription to both print out that string.

92
00:06:55,230 --> 00:06:57,400
Now of course I spelled this wrong subscriptions.

93
00:06:57,400 --> 00:06:58,210
There we go.

94
00:06:58,660 --> 00:07:05,170
But as you can tell a behavior subject gets the initial value or I should say the most recent value

95
00:07:05,740 --> 00:07:07,810
and then anything after the subscription.

96
00:07:07,810 --> 00:07:09,290
It can also access.

97
00:07:09,340 --> 00:07:15,280
So that is what differentiates a behavior subject from a published subject as you can see there a little

98
00:07:15,280 --> 00:07:20,360
bit different and there are different use cases for each one where one might make more sense than another.

99
00:07:20,380 --> 00:07:26,530
So that is one thing that makes behavior subjects unique is that they take an initial value and they

100
00:07:26,530 --> 00:07:33,330
also pay attention to the most recent value and then focus on anything else that comes after the subscription.

101
00:07:33,400 --> 00:07:38,850
So this is behavior subjects we're going to move on next to variables which is a type of observer.

102
00:07:38,860 --> 00:07:40,620
So I'll see in the next video.
