1
00:00:08,080 --> 00:00:10,990
Everybody what's going on this is Caleb with slopestyle.

2
00:00:11,200 --> 00:00:16,930
And in this video we're going to talk about observables and observables are sort of the foundational

3
00:00:16,930 --> 00:00:22,240
building blocks of our acts and our swift and so in order to begin.

4
00:00:22,240 --> 00:00:24,550
Go ahead and pull open your tricks playground.

5
00:00:24,550 --> 00:00:29,440
The one that we built in the last video and before we began we're actually going to go ahead and select

6
00:00:29,470 --> 00:00:32,240
all these other playgrounds because we're not going to use them.

7
00:00:32,380 --> 00:00:37,710
And I'm just going to actually move them to the trash sometimes X code gets grumpy about that.

8
00:00:37,720 --> 00:00:42,930
But all you need to do is reopen X code as you can see it just crashed.

9
00:00:43,120 --> 00:00:46,420
And I'm going to go ahead and pull it open again.

10
00:00:46,630 --> 00:00:47,450
There we go.

11
00:00:47,560 --> 00:00:51,030
And now we have just our singular playground.

12
00:00:51,070 --> 00:00:55,540
So let's go ahead let's start by talking about observables.

13
00:00:55,540 --> 00:01:04,510
Now everything in our ex-wifes is either an observable sequence or something that subscribes to events

14
00:01:04,570 --> 00:01:10,200
emitted by one of those observable sequences and that might be a lot of jargon and a lot of words maybe

15
00:01:10,210 --> 00:01:13,350
you're not familiar with but I'm going to show you how it works.

16
00:01:13,360 --> 00:01:20,950
We're going to start by creating our first observable case so let's just go ahead and type let number

17
00:01:20,950 --> 00:01:29,630
sequence equals observable and in order to create an observable for a single value you can type observable

18
00:01:30,000 --> 00:01:33,250
not just meaning just this one thing.

19
00:01:33,550 --> 00:01:36,950
And let's go ahead let's pass in the number five.

20
00:01:37,300 --> 00:01:37,540
OK.

21
00:01:37,560 --> 00:01:43,750
So now we have an observable with a value of 5 K that's pretty cool and the coolest thing is that it

22
00:01:43,750 --> 00:01:51,420
can infer what kind of observable this is so I want to show you one other thing you can type maybe let

23
00:01:51,460 --> 00:01:53,390
number equals observable.

24
00:01:53,560 --> 00:02:00,160
And using these brackets you can go ahead and set it to have an explicit type.

25
00:02:00,310 --> 00:02:06,260
But the cool thing is with swift type inference it allows us to infer that type automatically.

26
00:02:06,370 --> 00:02:13,210
And of course you could have gone in and you know done just what we did here with an integer not just

27
00:02:13,510 --> 00:02:17,580
that would create the same thing but no need to if Swift can infer it.

28
00:02:17,590 --> 00:02:22,800
So very cool although it is helpful to know if you need to create a specific type of absorbable maybe

29
00:02:22,840 --> 00:02:25,750
for C.G. float or for another type of value.

30
00:02:25,750 --> 00:02:29,470
So that is how we're going to create an observable.

31
00:02:29,470 --> 00:02:36,370
Now I want to show you how you can subscribe to this observable and do something based on how the values

32
00:02:36,370 --> 00:02:36,880
change.

33
00:02:36,880 --> 00:02:41,200
Or in this case how they have been added.

34
00:02:41,200 --> 00:02:50,310
So let's go ahead and let's say let number observer or maybe subscription.

35
00:02:51,050 --> 00:02:54,420
And we're going to set that to be equal to number sequence.

36
00:02:54,500 --> 00:02:58,010
But what we're going to do is we're going to call DOT subscribe.

37
00:02:58,310 --> 00:02:58,920
OK.

38
00:02:59,150 --> 00:03:02,710
And we're going to subscribe to number sequence.

39
00:03:02,750 --> 00:03:09,020
That means that we're setting up a property that's going to be able to monitor what happens in this

40
00:03:09,020 --> 00:03:10,460
observable sequence.

41
00:03:10,520 --> 00:03:16,130
But there are a couple different things we can do upon subscribing.

42
00:03:16,280 --> 00:03:25,880
OK so I want to show you that you can see here subscribe is on NEXT ON ERROR uncompleted and on disposed.

43
00:03:25,880 --> 00:03:29,860
Now on next basically means when a value comes in.

44
00:03:30,080 --> 00:03:39,200
So if I add five here on next would be called after on next is called If there's no error on completed

45
00:03:39,200 --> 00:03:40,210
will be called.

46
00:03:40,280 --> 00:03:44,840
And so I want to show you how these work what we can do is for.

47
00:03:44,960 --> 00:03:47,570
Well you know what let's just do on next first.

48
00:03:47,630 --> 00:03:52,300
And so I'm going to actually get rid of all of this and just type on next.

49
00:03:52,460 --> 00:03:54,500
And then we're going to do a closure here.

50
00:03:54,620 --> 00:04:01,370
So basically on next we're going to print whatever the value is and in order to get into that we can

51
00:04:01,370 --> 00:04:03,580
use the dollar sign zero.

52
00:04:03,770 --> 00:04:08,990
And what that's going to do is it's basically creating a temporary variable and it's cycling through

53
00:04:09,050 --> 00:04:11,730
this sequence of one value.

54
00:04:11,720 --> 00:04:16,520
It's only a singular value and I'll show you how to make an observable of multiple values in just a

55
00:04:16,520 --> 00:04:17,290
moment.

56
00:04:17,360 --> 00:04:22,020
But for now what we can do is we can print out whatever is in here.

57
00:04:22,190 --> 00:04:26,010
Now you can see it prints out five down here at the bottom.

58
00:04:26,030 --> 00:04:31,290
What if I were to change it to 10 it would print out 10 k.

59
00:04:31,550 --> 00:04:35,710
So observable that justice for making an observable of a single value.

60
00:04:35,960 --> 00:04:37,370
And we have subscribed to it.

61
00:04:37,370 --> 00:04:43,150
Now did you notice the number sequence by itself doesn't do anything.

62
00:04:43,220 --> 00:04:51,920
Only when we subscribe to the sequence and call on next or on completed or on disposed will it do something.

63
00:04:51,930 --> 00:04:54,370
Now that is really interesting to know.

64
00:04:54,490 --> 00:05:00,650
And we're going to see how some of these other capabilities like on completed and on Error work.

65
00:05:00,650 --> 00:05:02,750
So let's create a new sequence.

66
00:05:02,750 --> 00:05:04,640
Let's go ahead and type let.

67
00:05:04,910 --> 00:05:06,380
Hello.

68
00:05:06,380 --> 00:05:09,310
Sequence equals observable.

69
00:05:09,530 --> 00:05:11,420
And we're going to use a new function.

70
00:05:11,520 --> 00:05:14,220
We're going to use observable from.

71
00:05:14,510 --> 00:05:17,760
And as you can see it's looking for a sequence or an array.

72
00:05:18,050 --> 00:05:21,020
So I'm going to go ahead and select the one that's looking for an array.

73
00:05:21,410 --> 00:05:22,990
And we're going to give it one.

74
00:05:23,030 --> 00:05:29,990
So type in some square brackets there and we're going to create a string for each character in hello

75
00:05:31,420 --> 00:05:40,250
like so h e l l o.

76
00:05:40,830 --> 00:05:47,490
And so now we have an observable that is observing this array we have created an observable from an

77
00:05:47,490 --> 00:05:47,960
array.

78
00:05:48,060 --> 00:05:55,120
That's what from as for now in order to do something with this we need to subscribe to this observable.

79
00:05:55,290 --> 00:06:06,010
So let's go ahead and type let Hello subscription wups subscription equals Hello sequence that subscribe.

80
00:06:06,120 --> 00:06:13,350
But what we're going to do is we are going to basically use our subscribe on call here and we're going

81
00:06:13,350 --> 00:06:20,520
to switch through all the different cases that can happen to see what happens in a normal subscription.

82
00:06:20,520 --> 00:06:26,190
So go ahead and press enter and we're going to call this event and we're going to make a switch that

83
00:06:26,190 --> 00:06:30,070
will switch through all the potential outcomes.

84
00:06:30,090 --> 00:06:34,630
So go ahead and type switch event and we have some cases here.

85
00:06:34,680 --> 00:06:41,240
So type case and let's see what is possible for us to access we can access next.

86
00:06:41,820 --> 00:06:44,200
We can access error.

87
00:06:44,340 --> 00:06:44,590
Okay.

88
00:06:44,610 --> 00:06:50,420
Remember on next on air case completed Okay.

89
00:06:50,670 --> 00:06:52,820
Now for each of these cases we need to do something.

90
00:06:52,830 --> 00:06:58,290
So for case next we're going to go ahead and print but we need to know what to print.

91
00:06:58,320 --> 00:07:05,130
We're going to create a variable here or a constant rather that can capture the value of whatever event

92
00:07:05,760 --> 00:07:07,010
is happening so go ahead.

93
00:07:07,020 --> 00:07:12,330
And in parentheses here we'll type let value and we can print out value.

94
00:07:12,360 --> 00:07:17,530
So remember on next we printed out the value here and it printed 10 right.

95
00:07:17,610 --> 00:07:22,110
We're going to do the same thing here except we're going to print out the individual characters in our

96
00:07:22,110 --> 00:07:25,700
observable because remember it's an asynchronous sequence.

97
00:07:25,770 --> 00:07:26,950
Pretty cool.

98
00:07:27,000 --> 00:07:34,020
Now for Aero we're going to do the same thing we're going to type let error and we'll print an error

99
00:07:34,260 --> 00:07:36,560
if there is one for completed.

100
00:07:36,600 --> 00:07:40,890
We don't need to print anything we just basically need to know we're done subscribing so we're just

101
00:07:40,890 --> 00:07:46,700
going to print what's inside the parentheses we'll print completed.

102
00:07:46,710 --> 00:07:48,180
So now that this is done.

103
00:07:48,210 --> 00:07:50,130
Watch what happens.

104
00:07:50,130 --> 00:07:57,660
As you can see we are going through our subscription here and we are printing the value of every next

105
00:07:57,660 --> 00:07:58,380
event.

106
00:07:58,560 --> 00:08:04,470
So what is happening is we are going through this and next or on next is being called We're printing

107
00:08:04,470 --> 00:08:11,160
the value so we're cycling through the first element in our array is H then E then L then Elvan o.

108
00:08:11,310 --> 00:08:17,100
And then when it's finished we are printing completed so you can see that every sequence once it gets

109
00:08:17,100 --> 00:08:23,280
to the end uncompleted is called unless there's an error and then the error would be printed which of

110
00:08:23,280 --> 00:08:24,510
course you could do something with.

111
00:08:24,510 --> 00:08:30,390
So that is just one way that these observable sequences are very very cool.

112
00:08:30,420 --> 00:08:34,030
You get a lot of really great control over what you're doing.

113
00:08:34,260 --> 00:08:39,570
And so these are just two ways we can create observables and we're going to be talking about different

114
00:08:39,570 --> 00:08:43,270
kinds of observables in the next section of this course.

115
00:08:43,350 --> 00:08:44,670
And they're called subjects.

116
00:08:44,730 --> 00:08:49,800
They're like observables but they work in specific ways so some are better for some things and some

117
00:08:49,800 --> 00:08:51,120
are better for other things.

118
00:08:51,120 --> 00:08:56,310
Now this is just a basic intro to observables and we're going to talk a little bit more about them in

119
00:08:56,310 --> 00:08:58,480
the next video and the video following that.

120
00:08:58,560 --> 00:09:04,140
But by the end you're going to have a good understanding of observables as you can see we are observing

121
00:09:04,140 --> 00:09:11,130
a certain value we're subscribing to that value and we can do stuff with the values inside by simply

122
00:09:11,130 --> 00:09:12,870
calling dot subscribe.

123
00:09:12,960 --> 00:09:16,350
Now in the next video we're going to talk about disposing.

124
00:09:16,410 --> 00:09:21,280
And that's part of the memory management part of our ex-wife that we need to to be on top of.

125
00:09:21,420 --> 00:09:23,060
But don't worry it's really easy.

126
00:09:23,100 --> 00:09:28,350
So let's go ahead and head over to the next video where we're going to talk about disposing an observer.

127
00:09:28,350 --> 00:09:29,330
I'll see you there.
