Ben's Series

Transcript

00:10

00:10

The next thing we want to do is be able to actual edit an existing record. You’ll notice now that if I click on a particular row, we do go to the Add screen, which is really more of an Add/Edit screen but we’ll see nothing is pre-populating. How do we go about fixing this?

00:27

00:27

Between videos, something I did is I went into the Person model that extends RealmObject and I added a property called ID. It’s a very simple string right now. In fact, it’s going to be a Guid or a universal ID that we convert to a string. Right now Realm does not support Guids. In case you don’t know, UUID is actually a data type, like so. We’re just going to use a simple string for now.

00:54

00:54

I also have added an event listener for when an item is tapped on our list view, we get the selected Person and we push a new PersonPage.

01:08

01:08

If I run the application after setting the breakpoint, we will see that we have unique U-U-I-Ds or Guids for each person that we have. If I go in here and select ID, you’ll see it’s this long, big string that it’s really hard to have a duplicate of. If I select another person, you’ll see that these are unique. I’m going to close those.

01:33

01:33

I’m going to take a look at our services. Specifically take a look at our IDBService. One of the things that we will probably want to do is update our class interface to have a method called GetPersonByID. We’ll just do a string like this. If I now go into our RealmDBService, again, we’ll have an issue with our Intellisense. If I hit alt return, I can then implement this missing method. I’m going to quickly implement this. I’m going to build upon the GetPeople method that we already have. I’m going to add this Where clause where the person’s ID equals the ID that is passed. I’m going to do a first or default here where default will be null if we don’t have any matches.

02:34

02:34

Next step, let’s go back to our PeoplePage code behind. We’ll need to pass something to the person page to allow it to know that, “hey, here’s a particular ID that you should look up.” In this case, I’m going to pass the ID of our Person. Then I’m going to go into our PersonPage code behind and I’m going to add a new constructor. This constructor is going to take ID as a parameter. Now, it’s also very important that we include our initialized component. I’m going to create a local reference. ID is a reserved word so I’m going to say PersonID. I’m going to create this field and I’m going to make it protected, and then I’m going to get rid of this.

03:27

03:27

The next thing we need to do is go into our AddEditPersonViewModel and set up a way for our view to pass the ID to it so it can initialize itself. I’m going to setup Public void init and I’m going to pass our ID. I’m going to create our init method with the ID. I’m going to get a reference for our model. I’m going to say, “FirstName equals model, LastName equals model.” And really we want this to be model.FirstName.

04:11

04:11

The next thing I’m going to do is override OnBindingContextChange method, if this.BindingContext does not equal null then I can say BindingContext as AddEditPersonViewModel init and then I can say PersonID.

04:36

04:36

Let’s see if I did everything right on the first time. We got our list, at least we didn’t break that so far. Now, if I click here, we have our breakpoint, I disable it, hit continue, and boom. We now have our first name and last name pre-populated. If I go back to David Ortinau we’ll see I got the record here.

05:00

05:00

Next thing we need to do is save our model back to our Realm database. I’m going to actually change from going to this local variable to a class one. I’m going to say model. Then I’m going to say SaveCommand = new Command and then I’m going to pass a very simple lambda expression. Say protected void. Then we’re going to say BDService.SavePersonModel.ID, FirstName, LastName. You’ll also notice I have since updated the SavePerson method of DBService to also accept an ID.

05:58

05:58

So now, I’m going to put a breakpoint here. I’m going to run our application. I’m going to hit Benjamin. I’ll see my name. I’m going to change it Ben because I’m not overly stuffy. I’m going to hit create. Make sure our DoSave command gets invoked. Move past the breakpoint. And now when I go back we’ll see my change has updated, like so. This is how we can update a RealmObject using Realm Xamarin.