Master Your Realm Xamarin – Delete a RealmObject

In this lesson

  • Add delete method to DB service
  • Add delete command to VM
  • Add delete button to view
  • Run remove action in a transaction



The last step in the CRUD process that we haven’t covered is Destroy. Let’s take a look at that. If we take a look, between videos, something I did was I updated the initialization method of our AddEditPersonViewModel to check to see if the ID is null. If the ID is null, then we create a new model and use that reference for when we do our save.


In addition to our Save command, we now want a Delete command. I’m going to type in ICommand DeleteCommand. Just for the record, we’re going to be horrible, mean programmers and we’re not going to bother prompting the user whether or not they want to delete the record, and what not. We’re just going to go ahead and do it. If they screwed up it’s their fault.


I’m going to say DeleteCommand = new Command. Here again, I’m going to use a very simple lambda expression. I’m going to say DoDelete. I’m going to go in here and set a return type to void. Also, I’m going to make this method protected. Ok. The next thing I want to do is say DBService.DeletePerson. We’re just going to pass the ID. Our interface doesn’t have this method described so I’m just going to use the nice Xamarin Studio refactoring tools to quickly implement this. Also, if I try to build, I’ll get a compile error because I haven’t updated our RealmDBService yet.


And to implement this DeletePerson method, we’re going to say RealmInstance.Remove and we’re going to get our person by ID, like so. That’s going to drive me nuts. Let’s see if I can’t rename that. Now if we compile, everything should be good. And it is.


The last thing we need to do is going into our PersonPage code behind and I’m going to add a Delete button in the upper right of our application. I’m going to say CreateDeleteButton so I can easily reference this method from both constructors. I can say ToolbarItems.Add new ToolbarItem. We’re going to look through the constructors here. I’m going to give it a name of Delete. We don’t have any fancy icons. I’m going to say the action is … and I’m going to say the Binding.Context as PersonPageViewModel or AddEditPersonViewModel.DeleteCommand.Execute. And then last, I’m not going to bother with those last two parameters. I’m probably missing a parenthesis. You can never have enough parenthesis. Ah, like I said, not enough parenthesis. Need more. And execute does not take zero parameters so we’re also going to pass null. Alright, I managed to flubbed my way through that.


If we go into our AddEditPersonViewModel, I’m going to put a breakpoint at DoDelete to make sure I wired that up completely. I wouldn’t put it past me to screw this up as well. So now, if I click, I see my delete button, I’m going to hit it. Looks like that’s firing off as it should. I’m going to let this resume and now we get a runtime error. If we look at the runtime error, we see, “Cannot remove RealmObject outside write transactions.”


Let’s fix that! How about we go ahead and set up a transaction with Realm. I’m going to cut and paste from the Realm website and I’m just going to change a few things here. RealmInstance BeginWrite. If we take a look at what I just copy and pasted, you’ll see that I have my using statement here, it’s getting a reference to the transaction and then I’m saying RealmInstance.Remove and then I’m committing the transaction, like so. What this will do is this will help make sure we don’t have two processes trying to do the same thing at the same time. If this fails it will rewind our transaction.


Now, let’s try this again. Hit delete. Didn’t get an error. And now when we come back, Zack is no longer with us. Well, I mean, he’s alive but he’s not in this address book. Anyways, that’s how you can complete the CRUD cycle and complete or destroy RealmObjects by using Realm Xamarin.