This workshop will be retired on May 31, 2020.
Updating and Deleting Data11:59 with Amit Bijlani
Update or delete a Realm object from the database.
So in our previous video, we successfully added items into our shopping lists. 0:00 So we basically persisted these items to our database and 0:06 we were successfully able to display them in our tableView controller. 0:12 So now there's couple of things missing in our functionality. 0:16 The first thing is we cannot delete items. 0:19 So let's see how we can delete items. 0:22 So first thing we need to do is instructs our tableView that 0:25 it can actually edit items. 0:30 So we'll implement the method can edit row and simply return true. 0:33 Next, we need to implement the method that says commit editing style. 0:41 So what is the editing style that we're going to commit? 0:49 Well, if the editing style is delete then we're actually going to delete that item. 0:52 So let's say let item equal items (indexPath.row). 0:59 So we're going to get this item from our results array and 1:07 then we're going to delete it from our realm database. 1:11 To delete it from the realm database, we have to once again use the transaction 1:14 block, the write transaction block, and we have to use the delete method. 1:20 So we said delete and we have to pass it the role model object 1:26 that we want to delete from the realm database. 1:30 Now remember whenever you do a write transaction block it throws an error 1:33 which means that we have to either put it in a do catch block Or force try it. 1:39 For now, I'm going to force try it but remember that in a real world 1:46 application you want to put in a do catch block and gracefully handle failure. 1:51 So here are the compilers complaining that we need 1:55 to insert override and we're gonna override that method. 2:00 So that's all the code that we need to write for 2:05 us to delete an item from our tableVew. 2:10 Let's see if this works, running our application, click on Edit, hit Delete. 2:14 And nothing happened and our app crashed. 2:26 Okay, so let's figure out what happened, 2:30 it says round exception reason index one is out of balance. 2:33 So what happened here? 2:40 Why did our app crash? 2:43 While we actually deleted it from the database but 2:47 we did not delete the roll from our tableView. 2:50 That's my bad. 2:54 So we'll say tableView.deleteRows and 2:55 we'll pass it an array of indexPath in our cases just one. 3:00 And simply fit this out. 3:05 All right, let us run it. 3:10 Well, so technically deleted those two rows from the database but 3:14 it did not reflect that in our user interface. 3:18 So let's see if we can delete bananas. 3:21 And there we go we successfully deleted all of three items from our tableView. 3:24 So we have insert, we have delete, and now we need update. 3:30 So lets say I put in an item that says I want bleach. 3:37 So lets say I wanted to update this to a specific bleach or 3:43 a specific brand of bleach, 3:47 instead of just calling it bleach, I want to say a like hey, I wanted to buy Clorox. 3:49 So I should be able to click on it and I should be able to enter those details 3:54 in the detail view controller and when I hit done it should update for me. 3:59 So let's see how we can make that happen, all right. 4:05 So for this we need the prepare for segue method. 4:11 I've already stubbed out the prepare for a segue over here. 4:15 Basically, I am getting the indexPath of the selected row then I retrieve 4:21 the item from the items results array and I get that. 4:27 And then, I'm basically getting the destination view controller and 4:33 then setting that to our destination view controller 4:37 to a property called detail item. 4:41 Now, I don't have this property, so let's go ahead and add this years we'll call it 4:44 var detail item and this is an optional off type item. 4:50 So in viewDidLoad called Super ViweDidLoad and 4:58 we'll call a method called configureView So lets see what configureView looks like. 5:05 So basically we want to unwrap that optional. 5:17 And then we'll simply set our text field. 5:23 Say a TextField.Text = item.title and 5:26 quantity.text = item.qty. 5:34 Okay, right, so quantity is an integer. 5:40 That's what this Compiler thing is complaining, it's saying cannot convert to 5:45 an optional string, so we have to convert it ourselves. 5:51 Okay? 5:59 So next, we want to update it. 6:00 So when the done button is tapped. 6:03 Instead of inserting a new item, we're going to update our item. 6:06 So let's say, if let item is equal to detail item. 6:10 Then update item, else 6:18 insert item. 6:22 So for a swift, instead of saying update item, insert item, 6:30 you can just say update item, insert item. 6:34 All right, so let's, since we have our insert code already, 6:39 let's create the insert method. 6:43 We'll say, insert item, Of type item. 6:45 And- Just need to put, the realm stuff here. 6:52 And once again instead of realm being here. 7:01 We can either put it inside our insert method or 7:05 we can simply make it a property of the view controller. 7:08 So any of the methods inside the view controllers can easily use it. 7:12 So next, we need an update:tem. 7:17 So when updating an item what we need to do is we need to actually pass in or 7:26 update the properties of the item inside a write block possible. 7:32 That's all we need to do with in the insert we should actually passing 7:37 the title and quantity instead off an items title. 7:41 And quantity is an integer. 7:45 So we'll say insert item with title quantity. 7:51 So now, we can copy this code and put it inside our insert item method. 7:59 Insert item. 8:12 Title and quantity. 8:14 All right, so there's our insert method we are inserting an item. 8:24 I'm just really fact doing all this code so 8:30 that it's easier to read when you look at the done tap method. 8:31 And we don't really have to do all this refactoring but 8:37 it's nice to have readable code, so that whoever is coming along and 8:41 takes a look at your code, they can easily tell what's going on inside your method. 8:45 So now, update with the title and quantity. 8:51 So we need to do this inside a realm transaction block. 9:02 So, I'm going to do a do try realm.write but 9:07 we need this item objects and 9:15 we have a detail item. 9:19 This right here which is a property of the DetailViewController, we have that. 9:23 So lets unwrap it we'll say, if let item = detailItem. 9:28 And inside this write transaction, we'll say item.title is equal to title. 9:42 And item.quantity is equal to quantity. 9:47 And if there is any problem we're going to catch that error and 9:53 we're going to print it out. 9:57 So in our done button we need to update the signature of this method. 10:03 So we'll say update with title quantity. 10:08 All right, so basically when the done button tapped, 10:16 we need to check to see if there is a detail item which means that 10:20 this is an update, if there isn't then we have to insert an item. 10:25 All right, we should call this update item or just update. 10:32 All right, the compiler is complaining because we never use the item, 10:38 so we should do detail not equals null. 10:43 So if the detail item is not equal to null then we're going to update an item and 10:47 if it is null then we're going to insert a new item into the database to see 10:51 how readable that is and how easy that is. 10:56 Going to change that pragma mark there. 11:02 I think that's it well let's run our application to see 11:04 if our update functionality is working. 11:08 So when I click on it says bleach, 11:11 Clorox and I hit done and there we go it has been updated and our delete 11:16 functionality still works if I wanna delete it I can delete it, wonderful. 11:24 So in a short period of time we have inserted data into the realm database, 11:29 we have displayed it in our tableView controller and 11:34 we have learned how to delete and update that data. 11:37 Next coming up, we're going to see how we can associate categories or 11:41 create relationships between two different objects. 11:45 So you can have a one to many, or many to many, or many to one kind of relationship. 11:49 So we're going to look at the one to many kind of relationship here. 11:54
You need to sign up for Treehouse in order to download course files.Sign up