0
1
00:00:00,820 --> 00:00:09,040
Now, in the last lesson, we looked at how we can update our Realm database by simply using realm.write, and including
1

2
00:00:09,070 --> 00:00:10,940
inside those brackets
2

3
00:00:11,110 --> 00:00:16,660
whatever changes we want to make to our Realm database. Now, in this lesson, we're going to talk about how
3

4
00:00:16,660 --> 00:00:18,310
we can delete items.
4

5
00:00:18,340 --> 00:00:25,060
So I'm going to do that in the same place in the didSelectRowAt indexPath method where we click on one
5

6
00:00:25,060 --> 00:00:32,290
of the rows and we look inside our to-do items to find the item at that particular index, and we pull
6

7
00:00:32,290 --> 00:00:38,310
it out. And instead of trying to update it and toggle the done property,
7

8
00:00:38,410 --> 00:00:46,390
instead, I'm going to comment out this line, and I'm simply going to say realm. delete, and I'm going to
8

9
00:00:46,390 --> 00:00:54,400
call the delete method on our realm to delete the object that I specify. And that object is, obviously,
9

10
00:00:54,400 --> 00:00:57,520
going to be our item that we picked out.
10

11
00:00:57,820 --> 00:01:03,970
So realm.write updates our database and the changes that it's going to make is it's going to delete
11

12
00:01:04,210 --> 00:01:06,430
this item from our realm database.
12

13
00:01:06,430 --> 00:01:11,650
So, again, we're going to go inside our Shopping to-do list where we've got all of our items.
13

14
00:01:11,650 --> 00:01:13,620
So currently, they're all unchecked.
14

15
00:01:13,690 --> 00:01:21,910
And when I click on any of these rows, I'm expecting to look at that indexPath of the row and look inside
15

16
00:01:21,910 --> 00:01:25,070
to -do items to pick out the item at that row.
16

17
00:01:25,420 --> 00:01:32,250
And then I'm going to update my Realm by deleting that particular item from my Realm database.
17

18
00:01:32,290 --> 00:01:33,700
So let's see if it works.
18

19
00:01:34,530 --> 00:01:37,550
By apples, by peaches, by pears, everything is gone
19

20
00:01:37,570 --> 00:01:39,160
every single time I click on it.
20

21
00:01:39,430 --> 00:01:44,880
So this is how easy it is to delete and update your realm.
21

22
00:01:45,220 --> 00:01:48,460
And I think it's far more readable for human as well.
22

23
00:01:48,470 --> 00:01:52,690
realm.write, realm.delete, none of this context and persistent container business.
23

24
00:01:52,690 --> 00:01:54,290
But, again, it's totally up to you
24

25
00:01:54,310 --> 00:01:59,590
which one you prefer. But I'm going to uncomment these two lines of code and I'm going to delete this
25

26
00:01:59,590 --> 00:02:06,250
line of code because I prefer the checking functionality as the user experience for our app.
26

27
00:02:06,250 --> 00:02:09,920
And I'm also going to delete all of these commented out lines of code.
27

28
00:02:10,120 --> 00:02:16,150
So, now we're able to load new to-do list items when we select any category.
28

29
00:02:16,330 --> 00:02:23,930
We're able to add new items and we're also able to update and delete items in our Realm database.
29

30
00:02:24,220 --> 00:02:31,060
So in the next lesson, we are going to query our database and sort out all of these lines of code that
30

31
00:02:31,060 --> 00:02:33,190
correspond to our search bar methods.
31

32
00:02:33,190 --> 00:02:35,980
So for all of that and more, see you on the next lesson.
