0
1
00:00:00,630 --> 00:00:00,970
All right.
1

2
00:00:00,990 --> 00:00:07,680
So we are now on to the last item in CRUD which is how do you delete data from your persistent container
2

3
00:00:07,820 --> 00:00:08,890
using Core Data.
3

4
00:00:09,030 --> 00:00:12,000
So let's try that out over here as well.
4

5
00:00:12,090 --> 00:00:19,140
So, what if instead of checking items off by just having the accessories show up, what if instead we just
5

6
00:00:19,140 --> 00:00:22,050
remove the item from a persistent container?
6

7
00:00:22,110 --> 00:00:28,800
So I'm going to comment out this line which is, of course, toggling the done attribute from true to false.
7

8
00:00:28,890 --> 00:00:32,040
And instead, I'm going to tap into that itemArray,
8

9
00:00:32,520 --> 00:00:41,190
and I'm going to use the method remove at a particular index, and the index is, of course, indexPath.row
9

10
00:00:41,190 --> 00:00:43,740
which is the current selected index.
10

11
00:00:43,740 --> 00:00:46,740
Now, remember this line does nothing for our Core Data.
11

12
00:00:46,800 --> 00:00:53,640
It merely updates our itemArray which is used to populate our table view so that when we reload the
12

13
00:00:53,640 --> 00:00:57,060
table view, we're able to refresh the latest items.
13

14
00:00:57,060 --> 00:01:06,390
Now, the method that will need to remove the data from our context is context.delete, and we can specify
14

15
00:01:06,690 --> 00:01:13,930
an NSManagedObject to delete, and that is, of course, going to be itemArray
15

16
00:01:14,090 --> 00:01:14,670
at indexPath.row,
16

17
00:01:14,880 --> 00:01:19,030
so the NSManagedObject at the current selected row here.
17

18
00:01:19,170 --> 00:01:23,440
Now, just saying delete to our context is not enough,
18

19
00:01:23,480 --> 00:01:30,930
remember, this is the temporary area. And unless we save that context and we commit those changes to our
19

20
00:01:30,930 --> 00:01:34,160
persistent container, then this line would have done nothing at all.
20

21
00:01:34,230 --> 00:01:40,440
So, now we're going to save that context using that same saveItem's method and that's going to take
21

22
00:01:40,440 --> 00:01:45,810
the current state of the context, and it's going to try and commit it to our persistent container.
22

23
00:01:45,840 --> 00:01:50,850
At the end of all of that, it's going to reload the table view and show us the latest data.
23

24
00:01:51,090 --> 00:01:59,070
Now the order which you call these two methods, one, is removing the current item from the itemArray
24

25
00:01:59,370 --> 00:02:05,880
which is used to load up the table view data source ,and the other one is removing the data from our permanent
25

26
00:02:05,880 --> 00:02:06,860
stores.
26

27
00:02:06,960 --> 00:02:09,270
The order matters a huge deal.
27

28
00:02:09,270 --> 00:02:11,760
And if we run our app, we'll see what happens
28

29
00:02:11,770 --> 00:02:13,480
when we don't take care of that.
29

30
00:02:13,650 --> 00:02:21,150
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

31
00:02:21,150 --> 00:02:28,580
are item array at the moment only contains three NSManagedObjects and they are at index 0, 1, and 2.
31

32
00:02:28,800 --> 00:02:35,070
Now when I click on the third row here or index equals 2, the first thing that's going to trigger is
32

33
00:02:35,070 --> 00:02:39,380
that I'm going to remove that item from our itemArray.
33

34
00:02:39,390 --> 00:02:46,560
So, now I only have two items in our itemArray. And if I click here, then our app is going to crash because
34

35
00:02:46,860 --> 00:02:49,670
indexPath.row at the moment equals 2.
35

36
00:02:49,830 --> 00:02:57,480
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

37
00:02:57,510 --> 00:03:03,600
then this is why we have an index out of range error. And if we try to run the app without clicking on
37

38
00:03:03,600 --> 00:03:11,520
the last item, if we try to click maybe the first item, then you can see there are  app also behaves rather strangely
38

39
00:03:11,940 --> 00:03:14,900
and it removes the second to last item.
39

40
00:03:15,120 --> 00:03:18,640
And this is all because of the order of our methods.
40

41
00:03:18,660 --> 00:03:26,610
So what we have to do is we have to call context.delete first to remove this NSManagedObject
41

42
00:03:26,640 --> 00:03:33,690
from our context before we try to remove it from our itemArray because we're using the itemArray here
42

43
00:03:33,720 --> 00:03:35,490
to try and grab that object.
43

44
00:03:35,490 --> 00:03:41,130
So, now if we run an app with this new order, then you can see that when we try to delete something, our
44

45
00:03:41,130 --> 00:03:43,220
app works as expected.
45

46
00:03:44,620 --> 00:03:48,290
So all of these work exactly as we want it to.
46

47
00:03:48,460 --> 00:03:53,130
So I think deleting our items outright when we click on it is a little bit dramatic.
47

48
00:03:53,200 --> 00:03:58,330
And to be honest, I think I prefer the user experience where we have a checkmark next to the ones that
48

49
00:03:58,330 --> 00:03:59,160
are done.
49

50
00:03:59,170 --> 00:04:01,790
So I'm going to comment out these two lines.
50

51
00:04:01,900 --> 00:04:08,710
But before we end this lesson, it's a good point to remind ourselves that to delete items from our context,
51

52
00:04:09,110 --> 00:04:15,520
all we need to do is just call context.delete, and then specify the item that we want removed.
52

53
00:04:15,610 --> 00:04:16,710
But that's not enough,
53

54
00:04:16,810 --> 00:04:23,740
as with everything using Core Data and trying to do CRUD on it, we have to also save the context and
54

55
00:04:23,740 --> 00:04:27,270
commit the current status to our persistent container.
55

56
00:04:27,310 --> 00:04:31,930
So other than read, everything else creates update or destroy.
56

57
00:04:31,990 --> 00:04:37,640
Basically, whenever you need to change the data inside the persistent store, you always need to call
57

58
00:04:37,640 --> 00:04:40,470
context.save to commit those changes.
58

59
00:04:40,540 --> 00:04:47,230
Now, when you're reading or as we've done inside loadItems, we don't call saveItems or context.save
59

60
00:04:47,230 --> 00:04:50,490
here because we don't need to change the persistent container.
60

61
00:04:50,590 --> 00:04:54,080
And instead, we're just fetching it and looking at the current version.
61

62
00:04:54,100 --> 00:04:59,850
Now, in the next lesson, we're going to see some more fancy stuff that we can do using Core Data,
62

63
00:05:00,110 --> 00:05:06,400
and I'm going to show you how you can query items using Core Data, so that we can create a search bar
63

64
00:05:06,700 --> 00:05:12,820
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

65
00:05:12,820 --> 00:05:14,520
for the ones that we're interested in.
65

66
00:05:14,680 --> 00:05:17,480
So for all of that and more, I'll see you on the next lesson.
