0 1 00:00:00,840 --> 00:00:08,660 So, now that we've managed to make our to-do list app work using Realm to create new items, to save new 1 2 00:00:08,670 --> 00:00:17,040 items, to read the data from our Realm database, it's time to tackle updating data in the database. And you'll 2 3 00:00:17,040 --> 00:00:24,330 find that similar to Core Data, updating data is very, very similar to saving a new item, 3 4 00:00:24,330 --> 00:00:27,710 and we're going to be using realm.write to do the same thing. 4 5 00:00:27,810 --> 00:00:34,820 So the place that makes the most sense to update are realm is, again, inside our didSelectRowAt 5 6 00:00:34,830 --> 00:00:36,090 indexPath Method. 6 7 00:00:36,090 --> 00:00:44,430 And this is the place where we select a cell inside our tableView in order to check or uncheck it by 7 8 00:00:44,460 --> 00:00:47,040 toggling this particular done property. 8 9 00:00:47,040 --> 00:00:52,500 So we're going to leave all of this commented out and I'm going to show you how you would do this using 9 10 00:00:52,500 --> 00:00:53,640 Realm. 10 11 00:00:53,700 --> 00:01:02,970 So what we can say is we can say if let item = todoItems, which, remember, is a container that 11 12 00:01:02,970 --> 00:01:10,290 contains a whole bunch of items and it's fetched from our realm, then we're going to grab the item at 12 13 00:01:10,350 --> 00:01:12,000 indexPath.row, 13 14 00:01:12,120 --> 00:01:13,800 so, the current selected row. 14 15 00:01:13,890 --> 00:01:22,650 And if this is not nil, then we're going to be able to access this Item object, and we can say 15 16 00:01:22,970 --> 00:01:23,950 try realm.write, 16 17 00:01:23,970 --> 00:01:31,200 and we're going to write item. equals the opposite of item.done. 17 18 00:01:31,530 --> 00:01:37,140 And because this can throw, then, again, we're going to wrap it inside a do catch block. 18 19 00:01:40,100 --> 00:01:50,650 And if there is an error, then we're going to say "Error saving done status." 19 20 00:01:50,770 --> 00:01:54,150 Let's just indent this for clarity sake. 20 21 00:01:54,160 --> 00:01:56,950 And now we have replaced this code. 21 22 00:01:56,980 --> 00:02:01,510 So just to run through it, we're checking to see if to-do list items is not nil, 22 23 00:02:01,840 --> 00:02:07,930 and if it's not, then we're going to pick out the item at indexPath.row and set it to equal item, 23 24 00:02:07,930 --> 00:02:15,070 and then we're going to try and write the updated property of item by toggling whatever it used to be before. 24 25 00:02:15,070 --> 00:02:17,980 So if it was false, it becomes true, true becomes false. 25 26 00:02:18,230 --> 00:02:25,450 And the last thing we need to do is, of course, call our trusty tableView.reloadData method to 26 27 00:02:25,450 --> 00:02:33,100 call this cellForRowAt indexPath method again and to update our cells based on this done property. 27 28 00:02:33,190 --> 00:02:34,320 And give it a check mark 28 29 00:02:34,330 --> 00:02:38,950 if it's done and remove the checkmark if it is not done. 29 30 00:02:38,950 --> 00:02:43,620 So all that's left for us to do is to run our app and see if it works. 30 31 00:02:43,630 --> 00:02:43,920 All right. 31 32 00:02:43,930 --> 00:02:50,320 So let's head into our Shopping category where we know we've got some items and let's select and say 32 33 00:02:50,320 --> 00:02:55,390 that we're done with buying peaches. And you can see that we've got a checkmark showing up and we can 33 34 00:02:55,390 --> 00:02:57,680 toggle these as we please. 34 35 00:02:57,810 --> 00:03:04,000 And if we have a look inside the Realm Browser, you'll see that as I check these, they get updated in 35 36 00:03:04,000 --> 00:03:06,920 real time and really, really fast as well. 36 37 00:03:07,000 --> 00:03:12,550 But when I check them over here, they will not get reflected in our table view because, as you remember, 37 38 00:03:13,030 --> 00:03:18,500 we cannot call reload table view from a different application into our app, 38 39 00:03:18,580 --> 00:03:20,660 so it has to happen over here. 39 40 00:03:20,800 --> 00:03:26,190 So that is how we can update our data using Realm. In the next lesson, 40 41 00:03:26,200 --> 00:03:29,260 we're going to talk about how to delete data using Realm. 41 42 00:03:29,290 --> 00:03:32,280 So all of that and more, see you on the next lesson.