Hey everybody this is Caleb with Loeb's dot com and in this fantastic video we're going to write a function
to remove objects from our core data persistent store.
We're going to remove them forever.
Going to get rid of them.
And we're also going to add the ability to delete them from our table cell.
We're going to be able to swipe tap delete.
And we're going to be able to remove it and obliterate it forever as well as reload our table view and
show all of the changes right away.
It's so cool.
So let's begin by pulling open that X code project and going into goals.
VC OK now we're going to go down to our extension and we're going to write a function to remove objects
from a managed context OK.
Now to do this go ahead and type phunk remove let's do remove goal K and we're going to remove a goal
at a certain index path right.
We want to pull it out from our array of managed objects so we're going to go ahead and say at index
path and we're going to pass an internal parameter here of index path which is a type index path lots
of index path there.
And we're going to go ahead and use that to remove our object.
Now we need to get another managed context here.
So we're going to create a reference to the managed context.
So let's do that by typing.
Guard let managed context equals app delegate persistent container view context and if we don't have
that we're going to just return it just like below.
We need just the reference to our managed context inside this function.
Next what we're going to do is we're going to call managed context.
Delete.
OK.
Now we need to delete a certain object.
And as you know this array of goals is of type goal which if you go to the declaration of goal let's
go find it.
We can see that it inherits from an S. managed object from core data.
So if we go back down we need to get a certain N-S managed object and to do that we're going to go into
the goals array and we're going to pull out the item at the index path dot row we ask OK we're going
to call this from our table view when we tap on a certain cell we're going to pass the index view of
that cell which is going to correspond exactly with the goal from the goals.
Because that's how the data is shown in the table view right.
And then we're going to call managed context not delete.
That's a function that's going to delete the goal at that and X path.
OK.
Now that is not enough though to actually delete what we need.
We need to call managed contexts save.
Do you remember in set when we save the function save we needed to settle this data.
But then in order for it to actually pass in we need to call save on the managed context.
We're going to do the same thing in remove We need to tell it we need to tell the manager context that
it needs to update itself.
So we're going to use a do and catch block in order to do this like you may have been expecting.
And all we're going to do is just say Try managed context save you probably noticed that said you probably
noticed that it said throws.
And so we're going to go ahead and Debug.
Print and we're going to say could not remove because that's what we're doing here and we're going to
take the error from the dot save function and we're going to call error that localize description that
will give us a little bit more information.
If there's a problem.
So let's say that we do successfully remove an object we're going to go ahead and just print successfully
removed goal all right it looks good.
So you know what guys this is all we need to do to remove something we need to find the item at the
certain index path we want to remove.
We're going to remove it from our managed context and we're going to save that managed context to update
everything.
That's all we've got to do to remove a goal.
It's that easy.
Next we need to enable our table view to be able to swipe and edit cells.
So there are a couple of different table view delegate methods we need to call.
We need to set a property called can edit row an index path.
So go ahead and call that can edit row at index path.
Believe it or not all we need to do to enable editing is call.
Or is return true.
Now we can edit our table view.
It's that easy.
Next what we're going to do is we're going to go ahead and set the editing style.
So we're going to go ahead and call editing style Foro an index path and we're going to return a table
view editing style.
And if you push if you put a period you can see there are a few different things.
There is insert delete and there is none.
Now insert is going to add a plus a green circle enclosing with a little plus sign.
Delete makes a red circle with a dash.
We don't want either of those to show up on our cells because we have kind of a custom configuration
here.
So we're going to just press none.
That way it doesn't show up anything special.
And so we can actually get rid of the table view cell editing style thing.
So our style has been saved.
Now we need to actually create the editing actions so we can edit it.
Now let's create the actions so let's go ahead and type edit actions forro at index path.
Very very cool.
So we're going to create two actions.
One is going to delete and one is going to add progress in this video we're only focusing on deleting.
So let's go ahead and create our delete action.
Go ahead and type let delete.
Action equals UI.
Table View row action.
Whoops row action.
And what we're going to do is put a parentheses there so that we can instantiate it properly first with
a style then with a title and last.
There's a handler at the end where we actually set what the action does.
So we're going to set the style to be destructive because destructive is what you use when data is going
to be destroyed.
The title is going to say delete.
OK we're going to use all caps just so it looks extra cool.
Deleting should be a very final thing.
And press enter on the handler.
And I want you to notice it's going to be returning a table view row action and an index path to us.
OK.
We're going to need the index path.
We won't need the row action but we still need to give it a name.
So go ahead and call RHO action WIPs action and the index path just type index path.
So this is where we're going to actually do the stuff when we push the delete button.
So in order to actually remove something we need to call self remove goal that function we just wrote
Where are we going to get the index path from here.
It's returned to us super easy.
So go ahead and type index path and we have now remove that goal and saved it.
Next what we need to do is we need to fetch all the data from our core data model because we've just
removed it.
It's changed.
Now we need to fetch it all and we need to update our table view.
So go ahead and call self fetch.
But you know what we don't want to go through all of this completion junk again.
So let's go ahead and let's find where we called it earlier here.
And we're going to actually put this into its own function so we can call it from anywhere in this in
this class.
So cut this and below we're going to call phunk fetch.
Fetch core data objects and pass that in there so we can now call fecche core data objects and that
will do the same thing that we need to as when we want to call it here.
So instead of calling the function from our extension we can call fetch core data objects and it does
the same exact thing.
We don't have to call on this big block of code.
This one here we don't have to call it twice.
We can just call it once with that function or we can call the same name twice.
We can write less code.
So now that we fetched all of the objects all the data is update and we can go ahead and call table
view delete rows at index path.
And what this is going to do is it's going to remove a certain row that we have deleted and then animate
closing it's really cool.
So it's asking for an array of index path.
So we're going to give it that an array of index path from the returned index path.
We're going to go ahead and give it an animation.
And you know what.
There are quite a few of them but we're just going to use Automattic.
OK that's just a nice little fade up it pulls all the ones below it up and kind of animates it down.
Very cool.
So that is our daily action that's what we want to happen when we push delete.
But we have not yet returned it to our table view yet we just have created it.
But first before we do that we need to set a background color because you can set the color of these
buttons to be whatever you want.
But I found that it's easy if you just type delete action.
Whoops delete action.
Background color.
And we're just going to use a color literal so tap on the color little double click it and we're going
to set it to be a nice red color.
Let's go ahead and let's do.
Ooh yeah.
You know that's nice.
So select mirror Shino.
That just means red I think.
And now that we have set the background color we can properly return our delete action.
OK but if I do that watch what happens it yells at me it's expecting an array.
So even though there's only one we need to put it inside of an array we're going to add a second one
later so it's OK.
So go ahead and build and run this well for now I'm just building it.
But now build and run.
And let's go check it out in our applet see if we can properly delete an item from Chordata calling
move goal removing the item at the index path fetching all of the objects after the data is changed
and reloading the rows.
So if we swipe we should be able to see our delete button.
Very cool.
Let's try to delete this one if I press it.
Watch what happens.
It deletes and I don't know if you saw that but it said successfully fetch data successfully removed
goal successfully fetched data.
So this is the initial loading that's when I removed the goal then it fetched the new data and deleted
the row it showed the animation.
Now you don't only have to swipe and press the button you can actually swipe all the way through and
it will delete it.
It's so cool that's just kind of an added thing that's in there for us so we can now successfully remove
any goals that we want and show our table view when it's done because remember we have conditional code.
If it's less then or if it's less than 1 if it's 0 it's going to hide our table just like that.
Super super cool guys.
This is amazing.
The only thing that we need to do next is to add a new action here for adding progress.
And at the very end when we meet our progress goal we need to set a UI view to fade in saying goal complete
We're going to do that in the next video and we will call this app finished amazing work.
Let's move on to that last video and let's get it started.