Everybody this is Kaila with slopes dot com and in this video we're going to talk about disposing k
now disposing and disposables are something used in Arctic's swift to make sure that observables and
subscriptions et cetera et cetera are released from memory so they don't get trapped.
So we don't get retain cycles and have memory issues now to begin.
We're going to actually talk about A-R C and ARCC something called automatic reference counting and
it's use to help track and monitor your apps memory usage the word automatic is key because it is automatically
making sure that things are released and bound when they need to be.
But sometimes it doesn't know exactly what to do.
And sometimes with our swift things can get stuck and that's why we're going to be using disposables.
We're going to dispose of our observable and basically free it up just tell the A-R see the automatic
reference counting hey we don't need this anymore.
You can get rid of it.
So we're going to go ahead and pull open our X playground.
And what we're going to do is we're going to find how first of all to dispose.
And second of all we're going to learn why why we need to do that.
So just a piece of information that I need to include about observables is that they can have zero or
more elements.
OK.
Now that's that's an important part that you need to know.
And as you saw we went through.
We called on next which when it is called it looks for the next value.
If it doesn't have a next value it prints complete it or if there's an error it prints error.
Now once an error or completed has been reached the observable can no longer produce any other elements
it kind of locks it in place it stops it where it's at and when completed or error is called it's going
to basically try to free up all of the internal resources if it's completed.
We don't need the memory space anymore.
If there's an error why use the memory space when we can just print the error and free up the resources
that we're using.
So that's where disposing comes into play.
Now when you are wanting to dispose what you can do is you can call hello subscription.
OK so wherever you have subscribed to your hello sequence you can call Dispose.
And what that's going to do is it's going to free up memory and you'll notice it doesn't call anything
it doesn't print anything but what it's going to do is it's going to dispose of whatever resource that
we're using and it can successfully because we get to a completed point hey that's really cool.
It would also do that if we were to get to an error.
So you can go ahead and call it this way although this is not recommended because you're calling it
explicitly right here on the subscription.
But what we can actually do instead of doing that is we can create something called a Dispose bag OK
and dispose bag is kind of just like a virtual garbage bag that whenever you free up resources they
just get tossed into the bag and then RC can come in and clean that up.
And so one of the cool things in our swift is basically Best practice is to use a Dispose bag and at
the end of every subscription we can add the subscription to the Dispose bag and it will automatically
be called whenever the subscription is done doing whatever it's doing.
So first we need to create a Dispose disposable.
So let's type let dispose bag equals dispose bag and we're going to go ahead and instantiate that.
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
to dispose bag.
OK.
Just like that.
And what this is going to do is as this sequence runs when it gets to the point where it needs to dispose.
Whoops let's make sure we're using the right dispose bag when it gets to the end of its subscription.
It's going to go ahead and automatically add itself to the Dispose bag and free up the resources for
us without us having to really do anything.
Think about it.
It's sort of automatic which is really cool.
Now we can do the same thing here add to dispose bag and we can call Dispose bag.
There we go.
And as you can see this has taken its time here to think but as it runs we will be able to see that
it will add to the Dispose bag.
It won't print anything but it'll continue running just as it's supposed to.
So as you can see the end of our functions here we call it add disposable to dispose bags so whenever
there's a disposable whenever it is created by our subscribed function it's going to get added to our
dispose bag.
Same with our Helo's subscription we're adding disposable here to the Dispose bag and again just think
of this sort of like a garbage bag that just is there.
And when we call add disposable too we can just chuck whatever disposables we have in the Dispose bag
and it does it for us.
Honestly this part of our act Swift is sort of brainless and it's best practice just to chuck it in
the Dispose bag because our Swift can automatically determine when things are deallocated and when he
needs to come in and clean it up.
So this is disposing and disposing is a best practice because otherwise you're going to get retained
cycles you can have memory leaks and you don't want that.
So basically rule of thumb at the end of a subscription we're going to go ahead and add our disposables
to our dispose bag.
OK.
That's that.
That is disposing and that is so that we can properly use ARCC automatic reference counting in ARC Swift.
Very very cool.
This is the basics of observables.
They're really not that complicated guys just think of them like a asynchronous sequence of values.
They could be integers it can be strings that can be custom types it can be anything anything can be
a an observable sequence in our swift which is really cool.
So we're going to go ahead and move on to subjects which are special types of observers and you're going
to learn a lot more about how observables work when you're dealing with subjects because they're sort
of like a custom observable that works in a very specific way.
So we're going to dive into that in just a moment.
But for now just go out and give yourself a pat on the back.
This is all really new really fresh ideas.
And so I just want you to take a moment and just kind of mull over what you've been learning.
Maybe try playing around with observables and see what you can figure out on your own.
Awesome job with this video and we'll see over in the next section where we're going to start talking
about subjects.