1
00:00:08,080 --> 00:00:14,050
Everybody this is Kaila with slopes dot com and in this video we're going to talk about disposing k

2
00:00:14,130 --> 00:00:21,810
now disposing and disposables are something used in Arctic's swift to make sure that observables and

3
00:00:21,810 --> 00:00:26,630
subscriptions et cetera et cetera are released from memory so they don't get trapped.

4
00:00:26,640 --> 00:00:31,680
So we don't get retain cycles and have memory issues now to begin.

5
00:00:31,680 --> 00:00:38,370
We're going to actually talk about A-R C and ARCC something called automatic reference counting and

6
00:00:38,370 --> 00:00:46,380
it's use to help track and monitor your apps memory usage the word automatic is key because it is automatically

7
00:00:46,380 --> 00:00:51,210
making sure that things are released and bound when they need to be.

8
00:00:51,390 --> 00:00:55,160
But sometimes it doesn't know exactly what to do.

9
00:00:55,200 --> 00:01:02,280
And sometimes with our swift things can get stuck and that's why we're going to be using disposables.

10
00:01:02,280 --> 00:01:09,810
We're going to dispose of our observable and basically free it up just tell the A-R see the automatic

11
00:01:09,810 --> 00:01:12,120
reference counting hey we don't need this anymore.

12
00:01:12,210 --> 00:01:13,440
You can get rid of it.

13
00:01:13,530 --> 00:01:16,970
So we're going to go ahead and pull open our X playground.

14
00:01:17,220 --> 00:01:23,910
And what we're going to do is we're going to find how first of all to dispose.

15
00:01:24,090 --> 00:01:28,710
And second of all we're going to learn why why we need to do that.

16
00:01:28,740 --> 00:01:38,280
So just a piece of information that I need to include about observables is that they can have zero or

17
00:01:38,280 --> 00:01:39,510
more elements.

18
00:01:39,510 --> 00:01:40,040
OK.

19
00:01:40,170 --> 00:01:43,050
Now that's that's an important part that you need to know.

20
00:01:43,350 --> 00:01:45,730
And as you saw we went through.

21
00:01:45,750 --> 00:01:50,340
We called on next which when it is called it looks for the next value.

22
00:01:50,340 --> 00:01:55,770
If it doesn't have a next value it prints complete it or if there's an error it prints error.

23
00:01:55,770 --> 00:02:04,340
Now once an error or completed has been reached the observable can no longer produce any other elements

24
00:02:04,350 --> 00:02:12,540
it kind of locks it in place it stops it where it's at and when completed or error is called it's going

25
00:02:12,540 --> 00:02:17,000
to basically try to free up all of the internal resources if it's completed.

26
00:02:17,130 --> 00:02:19,420
We don't need the memory space anymore.

27
00:02:19,440 --> 00:02:25,110
If there's an error why use the memory space when we can just print the error and free up the resources

28
00:02:26,370 --> 00:02:27,120
that we're using.

29
00:02:27,120 --> 00:02:31,140
So that's where disposing comes into play.

30
00:02:31,170 --> 00:02:39,730
Now when you are wanting to dispose what you can do is you can call hello subscription.

31
00:02:39,750 --> 00:02:46,180
OK so wherever you have subscribed to your hello sequence you can call Dispose.

32
00:02:46,410 --> 00:02:51,360
And what that's going to do is it's going to free up memory and you'll notice it doesn't call anything

33
00:02:51,360 --> 00:02:56,370
it doesn't print anything but what it's going to do is it's going to dispose of whatever resource that

34
00:02:56,370 --> 00:03:02,850
we're using and it can successfully because we get to a completed point hey that's really cool.

35
00:03:02,850 --> 00:03:05,370
It would also do that if we were to get to an error.

36
00:03:05,370 --> 00:03:11,250
So you can go ahead and call it this way although this is not recommended because you're calling it

37
00:03:11,250 --> 00:03:14,150
explicitly right here on the subscription.

38
00:03:14,280 --> 00:03:21,540
But what we can actually do instead of doing that is we can create something called a Dispose bag OK

39
00:03:21,870 --> 00:03:27,870
and dispose bag is kind of just like a virtual garbage bag that whenever you free up resources they

40
00:03:27,870 --> 00:03:32,000
just get tossed into the bag and then RC can come in and clean that up.

41
00:03:32,010 --> 00:03:38,880
And so one of the cool things in our swift is basically Best practice is to use a Dispose bag and at

42
00:03:38,880 --> 00:03:44,280
the end of every subscription we can add the subscription to the Dispose bag and it will automatically

43
00:03:44,280 --> 00:03:48,270
be called whenever the subscription is done doing whatever it's doing.

44
00:03:48,450 --> 00:03:50,730
So first we need to create a Dispose disposable.

45
00:03:50,760 --> 00:03:58,860
So let's type let dispose bag equals dispose bag and we're going to go ahead and instantiate that.

46
00:03:58,890 --> 00:04:04,170
Now what I can do is I can go ahead and go to the end of my subscription here and I can type dot add

47
00:04:04,170 --> 00:04:06,150
to dispose bag.

48
00:04:06,480 --> 00:04:06,800
OK.

49
00:04:06,810 --> 00:04:07,820
Just like that.

50
00:04:07,950 --> 00:04:14,600
And what this is going to do is as this sequence runs when it gets to the point where it needs to dispose.

51
00:04:14,620 --> 00:04:19,830
Whoops let's make sure we're using the right dispose bag when it gets to the end of its subscription.

52
00:04:19,830 --> 00:04:24,450
It's going to go ahead and automatically add itself to the Dispose bag and free up the resources for

53
00:04:24,450 --> 00:04:26,790
us without us having to really do anything.

54
00:04:26,790 --> 00:04:27,610
Think about it.

55
00:04:27,690 --> 00:04:30,100
It's sort of automatic which is really cool.

56
00:04:30,120 --> 00:04:35,680
Now we can do the same thing here add to dispose bag and we can call Dispose bag.

57
00:04:36,030 --> 00:04:36,830
There we go.

58
00:04:37,200 --> 00:04:44,610
And as you can see this has taken its time here to think but as it runs we will be able to see that

59
00:04:44,610 --> 00:04:46,250
it will add to the Dispose bag.

60
00:04:46,260 --> 00:04:50,610
It won't print anything but it'll continue running just as it's supposed to.

61
00:04:50,610 --> 00:04:58,410
So as you can see the end of our functions here we call it add disposable to dispose bags so whenever

62
00:04:58,410 --> 00:05:03,450
there's a disposable whenever it is created by our subscribed function it's going to get added to our

63
00:05:03,450 --> 00:05:04,820
dispose bag.

64
00:05:04,890 --> 00:05:10,370
Same with our Helo's subscription we're adding disposable here to the Dispose bag and again just think

65
00:05:10,370 --> 00:05:12,760
of this sort of like a garbage bag that just is there.

66
00:05:12,800 --> 00:05:18,830
And when we call add disposable too we can just chuck whatever disposables we have in the Dispose bag

67
00:05:19,010 --> 00:05:19,970
and it does it for us.

68
00:05:19,970 --> 00:05:24,650
Honestly this part of our act Swift is sort of brainless and it's best practice just to chuck it in

69
00:05:24,650 --> 00:05:31,340
the Dispose bag because our Swift can automatically determine when things are deallocated and when he

70
00:05:31,430 --> 00:05:32,720
needs to come in and clean it up.

71
00:05:32,720 --> 00:05:39,290
So this is disposing and disposing is a best practice because otherwise you're going to get retained

72
00:05:39,290 --> 00:05:41,900
cycles you can have memory leaks and you don't want that.

73
00:05:41,900 --> 00:05:48,350
So basically rule of thumb at the end of a subscription we're going to go ahead and add our disposables

74
00:05:48,590 --> 00:05:49,840
to our dispose bag.

75
00:05:50,030 --> 00:05:50,550
OK.

76
00:05:50,780 --> 00:05:51,320
That's that.

77
00:05:51,320 --> 00:05:58,080
That is disposing and that is so that we can properly use ARCC automatic reference counting in ARC Swift.

78
00:05:58,160 --> 00:05:59,390
Very very cool.

79
00:05:59,390 --> 00:06:01,550
This is the basics of observables.

80
00:06:01,550 --> 00:06:08,460
They're really not that complicated guys just think of them like a asynchronous sequence of values.

81
00:06:08,460 --> 00:06:13,820
They could be integers it can be strings that can be custom types it can be anything anything can be

82
00:06:13,880 --> 00:06:17,280
a an observable sequence in our swift which is really cool.

83
00:06:17,540 --> 00:06:23,390
So we're going to go ahead and move on to subjects which are special types of observers and you're going

84
00:06:23,390 --> 00:06:28,190
to learn a lot more about how observables work when you're dealing with subjects because they're sort

85
00:06:28,190 --> 00:06:31,900
of like a custom observable that works in a very specific way.

86
00:06:31,910 --> 00:06:34,110
So we're going to dive into that in just a moment.

87
00:06:34,250 --> 00:06:37,160
But for now just go out and give yourself a pat on the back.

88
00:06:37,160 --> 00:06:39,910
This is all really new really fresh ideas.

89
00:06:40,070 --> 00:06:43,880
And so I just want you to take a moment and just kind of mull over what you've been learning.

90
00:06:44,000 --> 00:06:47,900
Maybe try playing around with observables and see what you can figure out on your own.

91
00:06:47,960 --> 00:06:51,890
Awesome job with this video and we'll see over in the next section where we're going to start talking

92
00:06:51,890 --> 00:06:52,970
about subjects.
