Now, in the last lesson, we looked at how we can update our Realm database by simply using realm.write, and including 1
inside those brackets 2
whatever changes we want to make to our Realm database. Now, in this lesson, we're going to talk about how 3
we can delete items. 4
So I'm going to do that in the same place in the didSelectRowAt indexPath method where we click on one 5
of the rows and we look inside our to-do items to find the item at that particular index, and we pull 6
it out. And instead of trying to update it and toggle the done property, 7
instead, I'm going to comment out this line, and I'm simply going to say realm. delete, and I'm going to 8
call the delete method on our realm to delete the object that I specify. And that object is, obviously, 9
going to be our item that we picked out. 10
So realm.write updates our database and the changes that it's going to make is it's going to delete 11
this item from our realm database. 12
So, again, we're going to go inside our Shopping to-do list where we've got all of our items. 13
So currently, they're all unchecked. 14
And when I click on any of these rows, I'm expecting to look at that indexPath of the row and look inside 15
to -do items to pick out the item at that row. 16
And then I'm going to update my Realm by deleting that particular item from my Realm database. 17
So let's see if it works. 18
By apples, by peaches, by pears, everything is gone 19
every single time I click on it. 20
So this is how easy it is to delete and update your realm. 21
And I think it's far more readable for human as well. 22
realm.write, realm.delete, none of this context and persistent container business. 23
But, again, it's totally up to you 24
which one you prefer. But I'm going to uncomment these two lines of code and I'm going to delete this 25
line of code because I prefer the checking functionality as the user experience for our app. 26
And I'm also going to delete all of these commented out lines of code. 27
So, now we're able to load new to-do list items when we select any category. 28
We're able to add new items and we're also able to update and delete items in our Realm database. 29
So in the next lesson, we are going to query our database and sort out all of these lines of code that 30
correspond to our search bar methods. 31
So for all of that and more, see you on the next lesson.