All right. 1
So we are now on to the last item in CRUD which is how do you delete data from your persistent container 2
using Core Data. 3
So let's try that out over here as well. 4
So, what if instead of checking items off by just having the accessories show up, what if instead we just 5
remove the item from a persistent container? 6
So I'm going to comment out this line which is, of course, toggling the done attribute from true to false. 7
And instead, I'm going to tap into that itemArray, 8
and I'm going to use the method remove at a particular index, and the index is, of course, indexPath.row 9
which is the current selected index. 10
Now, remember this line does nothing for our Core Data. 11
It merely updates our itemArray which is used to populate our table view so that when we reload the 12
table view, we're able to refresh the latest items. 13
Now, the method that will need to remove the data from our context is context.delete, and we can specify 14
an NSManagedObject to delete, and that is, of course, going to be itemArray 15
at indexPath.row, 16
so the NSManagedObject at the current selected row here. 17
Now, just saying delete to our context is not enough, 18
remember, this is the temporary area. And unless we save that context and we commit those changes to our 19
persistent container, then this line would have done nothing at all. 20
So, now we're going to save that context using that same saveItem's method and that's going to take 21
the current state of the context, and it's going to try and commit it to our persistent container. 22
At the end of all of that, it's going to reload the table view and show us the latest data. 23
Now the order which you call these two methods, one, is removing the current item from the itemArray 24
which is used to load up the table view data source ,and the other one is removing the data from our permanent 25
stores. 26
The order matters a huge deal. 27
And if we run our app, we'll see what happens 28
when we don't take care of that. 29
If we run our app as it is right now and we click on the last item in our table view, so remember, there 30
are item array at the moment only contains three NSManagedObjects and they are at index 0, 1, and 2. 31
Now when I click on the third row here or index equals 2, the first thing that's going to trigger is 32
that I'm going to remove that item from our itemArray. 33
So, now I only have two items in our itemArray. And if I click here, then our app is going to crash because 34
indexPath.row at the moment equals 2. 35
And once we remove the item at index 2 and then we try to access it to try and delete it from our context, 36
then this is why we have an index out of range error. And if we try to run the app without clicking on 37
the last item, if we try to click maybe the first item, then you can see there are app also behaves rather strangely 38
and it removes the second to last item. 39
And this is all because of the order of our methods. 40
So what we have to do is we have to call context.delete first to remove this NSManagedObject 41
from our context before we try to remove it from our itemArray because we're using the itemArray here 42
to try and grab that object. 43
So, now if we run an app with this new order, then you can see that when we try to delete something, our 44
app works as expected. 45
So all of these work exactly as we want it to. 46
So I think deleting our items outright when we click on it is a little bit dramatic. 47
And to be honest, I think I prefer the user experience where we have a checkmark next to the ones that 48
are done. 49
So I'm going to comment out these two lines. 50
But before we end this lesson, it's a good point to remind ourselves that to delete items from our context, 51
all we need to do is just call context.delete, and then specify the item that we want removed. 52
But that's not enough, 53
as with everything using Core Data and trying to do CRUD on it, we have to also save the context and 54
commit the current status to our persistent container. 55
So other than read, everything else creates update or destroy. 56
Basically, whenever you need to change the data inside the persistent store, you always need to call 57
context.save to commit those changes. 58
Now, when you're reading or as we've done inside loadItems, we don't call saveItems or context.save 59
here because we don't need to change the persistent container. 60
And instead, we're just fetching it and looking at the current version. 61
Now, in the next lesson, we're going to see some more fancy stuff that we can do using Core Data, 62
and I'm going to show you how you can query items using Core Data, so that we can create a search bar 63
inside our to-do list app, so that if we have a whole bunch of to-dos like 20, we can search and look 64
for the ones that we're interested in. 65
So for all of that and more, I'll see you on the next lesson.