0
1
00:00:00,640 --> 00:00:06,550
In the last few lessons, we've managed to make our app be able to create data in our database, as well
1

2
00:00:06,550 --> 00:00:09,270
as read the data that exists in our database.
2

3
00:00:09,370 --> 00:00:13,960
And now we're going to tackle the "U" that's updating data into our database.
3

4
00:00:13,990 --> 00:00:19,900
The perfect place to look at this is, of course, the part where we update that done property, and that's
4

5
00:00:19,900 --> 00:00:22,270
when we click and select each item.
5

6
00:00:22,270 --> 00:00:28,520
Now, currently, what we're doing is that we're tapping into the current item by using the
6

7
00:00:28,580 --> 00:00:29,710
indexPath.row property.
7

8
00:00:29,710 --> 00:00:35,890
So, for example, if I clicked on this first row, then the indexPath.row would be equal to zero.
8

9
00:00:36,130 --> 00:00:44,140
And I would go inside the itemArray which we know is a array of NSManagedObjects and I would retrieve
9

10
00:00:44,290 --> 00:00:46,700
the first item inside that array.
10

11
00:00:46,960 --> 00:00:52,990
And then I would modify it's done property or done attribute to be the opposite of what it used to be.
11

12
00:00:52,990 --> 00:00:55,310
So true turns false, false turns to true.
12

13
00:00:55,550 --> 00:01:02,980
And then I commit those changes by using the saveItem's method which simply commits the current state
13

14
00:01:03,040 --> 00:01:07,960
of the context with that updated attribute to our persistent container.
14

15
00:01:07,960 --> 00:01:10,020
Now, there's other methods that you can use.
15

16
00:01:10,030 --> 00:01:17,950
For example, let's grab the current selected NSManagedObject and we can say .setValue
16

17
00:01:17,950 --> 00:01:18,670
forKey.
17

18
00:01:18,910 --> 00:01:23,290
So if we know that the key that we want to change was the title, for example,
18

19
00:01:23,290 --> 00:01:23,650
right?
19

20
00:01:23,800 --> 00:01:30,670
And we want to change the title to completed for every single completed task.
20

21
00:01:30,700 --> 00:01:33,220
So maybe your task used to say "Save the World!"
21

22
00:01:33,340 --> 00:01:36,490
but when you click on it, it now is going to read "Completed."
22

23
00:01:36,610 --> 00:01:40,960
Then this is a method that you can use to update your NSManageObject as well.
23

24
00:01:40,990 --> 00:01:46,330
But remember that no matter how you decide to update your NSManageObject, you still always have to
24

25
00:01:46,330 --> 00:01:48,180
call context.save.
25

26
00:01:48,340 --> 00:01:53,860
And that's because we're doing all of our changes, all of our creating, updating, reading, and destroying
26

27
00:01:54,040 --> 00:01:56,520
inside our temporary context.
27

28
00:01:56,560 --> 00:02:02,710
And it's only once we're happy with all those changes that we call context.save and commits those changes
28

29
00:02:02,950 --> 00:02:04,360
to our permanent container.
29

30
00:02:04,450 --> 00:02:10,240
So I'm going to keep my code here as it was before because it's more succinct than using a whole bunch
30

31
00:02:10,240 --> 00:02:13,490
of "if else" statements and trying to use set value forKey.
31

32
00:02:13,510 --> 00:02:16,810
This does exactly the same thing but in far fewer lines
32

33
00:02:16,840 --> 00:02:18,540
and it also looks a lot neater.
33

34
00:02:18,640 --> 00:02:23,560
So in the next lesson, we're going to talk about the last item in crud which is how do you delete items
34

35
00:02:23,680 --> 00:02:26,810
from your database using Core Data.
35

36
00:02:26,830 --> 00:02:30,000
So for all of that and more, I'll see you on the next lesson.
