In the last few lessons, we've managed to make our app be able to create data in our database, as well 1
as read the data that exists in our database. 2
And now we're going to tackle the "U" that's updating data into our database. 3
The perfect place to look at this is, of course, the part where we update that done property, and that's 4
when we click and select each item. 5
Now, currently, what we're doing is that we're tapping into the current item by using the 6
indexPath.row property. 7
So, for example, if I clicked on this first row, then the indexPath.row would be equal to zero. 8
And I would go inside the itemArray which we know is a array of NSManagedObjects and I would retrieve 9
the first item inside that array. 10
And then I would modify it's done property or done attribute to be the opposite of what it used to be. 11
So true turns false, false turns to true. 12
And then I commit those changes by using the saveItem's method which simply commits the current state 13
of the context with that updated attribute to our persistent container. 14
Now, there's other methods that you can use. 15
For example, let's grab the current selected NSManagedObject and we can say .setValue 16
forKey. 17
So if we know that the key that we want to change was the title, for example, 18
right? 19
And we want to change the title to completed for every single completed task. 20
So maybe your task used to say "Save the World!" 21
but when you click on it, it now is going to read "Completed." 22
Then this is a method that you can use to update your NSManageObject as well. 23
But remember that no matter how you decide to update your NSManageObject, you still always have to 24
call context.save. 25
And that's because we're doing all of our changes, all of our creating, updating, reading, and destroying 26
inside our temporary context. 27
And it's only once we're happy with all those changes that we call context.save and commits those changes 28
to our permanent container. 29
So I'm going to keep my code here as it was before because it's more succinct than using a whole bunch 30
of "if else" statements and trying to use set value forKey. 31
This does exactly the same thing but in far fewer lines 32
and it also looks a lot neater. 33
So in the next lesson, we're going to talk about the last item in crud which is how do you delete items 34
from your database using Core Data. 35
So for all of that and more, I'll see you on the next lesson.