1
00:00:08,070 --> 00:00:13,350
Hey everybody this is Caillat with slopes Loeb's dot com and welcome back to the Eric swift course in

2
00:00:13,440 --> 00:00:20,380
this section we're going to talk about subjects now subjects are a special form of observable.

3
00:00:20,450 --> 00:00:25,500
There observables but they have a special function special ability each one is a little bit different

4
00:00:25,590 --> 00:00:26,730
from the other.

5
00:00:26,970 --> 00:00:33,180
Of course just like always you can subscribe and dynamically add elements to subjects just like you

6
00:00:33,180 --> 00:00:34,860
can with observables.

7
00:00:34,860 --> 00:00:38,760
Now there are currently four kinds of subjects in ARC Swift.

8
00:00:38,760 --> 00:00:42,810
We're only going to be talking about three of them but they're pretty cool.

9
00:00:42,810 --> 00:00:47,880
So we're going to talk about publish subjects in this video and then in the next video we'll talk about

10
00:00:47,880 --> 00:00:53,070
behavior subjects and after behavior subjects we're going to talk about variables and you're probably

11
00:00:53,070 --> 00:00:56,280
thinking wait variables that's a normal part of swift.

12
00:00:56,280 --> 00:00:56,790
You're right.

13
00:00:56,790 --> 00:01:01,020
But a variable in our swift is a little bit different.

14
00:01:01,020 --> 00:01:07,680
So we're going to begin by talking about published subjects so go ahead and pull open your artics playground

15
00:01:08,190 --> 00:01:11,680
go ahead and right click on one of the playground pages and click.

16
00:01:11,790 --> 00:01:13,250
New playground page.

17
00:01:13,350 --> 00:01:17,380
We're going to call this publish subject.

18
00:01:17,820 --> 00:01:20,880
And let's go ahead and select all and delete it.

19
00:01:20,880 --> 00:01:24,330
Of course we need to import our acts Swift.

20
00:01:24,330 --> 00:01:30,490
We need to import playground support and we need to call playground page.

21
00:01:30,540 --> 00:01:34,350
Current DOT needs indefinite.

22
00:01:34,500 --> 00:01:40,550
In definite execution and set that to be equal to true.

23
00:01:40,650 --> 00:01:44,940
After that we're going to go ahead and create our very first publish subject.

24
00:01:44,940 --> 00:01:53,250
So go ahead and type var publish subject whip's publish subject and set it to be equal to type published

25
00:01:53,250 --> 00:01:54,210
subject.

26
00:01:54,240 --> 00:01:58,710
Now something you need to know is when you create a published subject you need to give it an explicit

27
00:01:58,710 --> 00:01:59,580
type.

28
00:01:59,580 --> 00:02:05,600
So we're gonna give it an explicit type of string and we're going to instantiate it by using two parentheses.

29
00:02:06,000 --> 00:02:10,860
Now what we can do with a publish subject is we can add values to it.

30
00:02:10,860 --> 00:02:11,720
Check it out.

31
00:02:11,850 --> 00:02:16,320
If I call publish subject make sure that you select the one with the lowercase p.

32
00:02:16,680 --> 00:02:22,450
And if I call dot on next I can pass in an element of string.

33
00:02:22,470 --> 00:02:28,800
So for instance I could say hello and now I have added an element to my published subject.

34
00:02:28,800 --> 00:02:31,590
Now you probably notice why isn't it doing anything.

35
00:02:31,710 --> 00:02:37,010
We haven't subscribed yet so let's go ahead and let's create a subscription.

36
00:02:37,380 --> 00:02:47,190
So go ahead and type let pub sub subscription and we're going to set that to be equal to publish subject

37
00:02:47,610 --> 00:02:50,390
and we're going to call DOT subscribe.

38
00:02:50,550 --> 00:02:54,090
Now we're going to call subscribe on next.

39
00:02:54,180 --> 00:02:55,450
So go ahead and type that.

40
00:02:55,830 --> 00:03:01,650
And then inside of these curly brackets we're going to do something we're going to go ahead and call

41
00:03:01,890 --> 00:03:08,630
print and we're going to use dollar signs 0 to print out whatever we have in our published subject.

42
00:03:08,640 --> 00:03:17,290
And as it runs you'll see it will print out the halo value that we have pushed in with on next.

43
00:03:17,570 --> 00:03:19,580
Now you're probably thinking well wait a minute.

44
00:03:19,610 --> 00:03:21,790
Why is nothing showing up.

45
00:03:21,800 --> 00:03:23,360
Why is nothing printing.

46
00:03:23,570 --> 00:03:30,410
The reason for that is because a published subject works in a very special way a published subject is

47
00:03:30,410 --> 00:03:38,900
going to show you all events but only the ones that occur after you subscribe.

48
00:03:38,900 --> 00:03:45,290
OK so anything that happens before a subscription will not be called so I can do this again.

49
00:03:45,290 --> 00:03:50,750
I could say publish subject on next and I could say world.

50
00:03:50,980 --> 00:03:56,510
You'll notice nothing gets printed but watch what happens after the subscription.

51
00:03:56,650 --> 00:04:05,370
If I type publish subject on next and I can say hello and as you can see it prints out hello because

52
00:04:05,370 --> 00:04:09,980
we added this using on next after we subscribed.

53
00:04:09,990 --> 00:04:17,050
Now I could do it again publish subject on next and I can say again hello again.

54
00:04:17,130 --> 00:04:22,980
So as you can see it prints out hello and again but it's ignoring everything that happens before we

55
00:04:22,980 --> 00:04:23,700
subscribed.

56
00:04:23,820 --> 00:04:26,720
So that is what makes a published subject unique.

57
00:04:26,820 --> 00:04:32,310
If you subscribe to it you will get all the events that are going to happen after the subscription.

58
00:04:32,310 --> 00:04:37,080
Now I want to create a new subscription here to show you what happens when you're dealing with multiple

59
00:04:37,080 --> 00:04:37,840
subscriptions.

60
00:04:37,840 --> 00:04:46,710
So go ahead and call Let pub sub subscription too and we're going to make a new subscription here.

61
00:04:46,730 --> 00:04:54,010
So using published subject let's say we want to use the same subject we can call DOT subscribe and we

62
00:04:54,010 --> 00:04:56,290
can call on next.

63
00:04:56,290 --> 00:05:02,260
Now using curly brackets I can go in here and inside the subscribe on next closure I can do the same

64
00:05:02,260 --> 00:05:02,580
thing.

65
00:05:02,590 --> 00:05:04,960
I can print dollar signs zero.

66
00:05:05,230 --> 00:05:10,210
Now you'll notice nothing is going to print for pub sub.

67
00:05:10,770 --> 00:05:21,370
But what I can do is call publish subject on next and I can say both subscriptions get this message

68
00:05:22,120 --> 00:05:23,490
and watch what happens.

69
00:05:24,390 --> 00:05:31,080
Both subscriptions printed because this happened after both subscriptions what happened before did not

70
00:05:31,080 --> 00:05:32,400
get to the first one.

71
00:05:32,610 --> 00:05:39,510
What happened after did not get to the second one because these were all before the second subscription.

72
00:05:39,510 --> 00:05:46,890
But if I call on next after both both subscriptions get the same message so there are lots of really

73
00:05:46,890 --> 00:05:49,720
cool ways that publish subjects can be used.

74
00:05:49,890 --> 00:05:53,100
It shows everything after a certain point of subscription.

75
00:05:53,100 --> 00:05:53,810
Pretty neat.

76
00:05:53,880 --> 00:05:57,590
So this is a brief overview of published subjects.

77
00:05:57,600 --> 00:06:02,840
It's a specific kind of observer where you can add in values you can make subscriptions.

78
00:06:03,060 --> 00:06:10,350
But the only values that will actually be noticed and used when subscribing are ones that occur after

79
00:06:10,440 --> 00:06:12,540
the subscription asynchronously.

80
00:06:12,570 --> 00:06:18,180
So this is published subjects in the next video we're going to talk about behavior subjects which are

81
00:06:18,210 --> 00:06:20,430
similar but quite different.

82
00:06:20,430 --> 00:06:22,230
So I'll see in the next video.

83
00:06:22,230 --> 00:06:24,260
Awesome job with this one guys.
