Master Your Xamarin Realm DB – RealmList for 1-To-Many Relationships

In this lesson

  • Update Person model to have RealmList of Addresses
  • Update IDBService to create addresses and add them to Person
  • Update VM to retrieve addresses
  • Update XAML to display addresses via bindings




Since our last video I’ve updated our address book application to include addresses in our person details screen, here as you can see. Now I have this list be here that will list out the different addresses that we will create for our different contacts. And we’ll see it’s pretty standard fare and we’ll see that when I click the save button we throw this NotImplementedException because we have our view bound to our AddEditAddressViewModel.


Create Address Model and Add RealmList to Person Model

So what did I do? Well one of the things I did is that I created a new model that extends RealmObject called Address. And we’ll see that I’ve specified this ID here as our object ID which will act as kind of a primary key of sorts. And then we’ve added your traditional address properties. And now I’m going to add a collection of these addresses to our previously created person model. You’ll see here I’m not using a standard C# List but instead I’m using a RealmList. And this will allow us to add and remove items from our addresses collection and have our Realm database be updated in the background.


Update IDBService to Include SaveAddress Method

So we’re gonna close all these. Next we need do is going to IDBService and update our SaveAddress method. As you can see here we’re gonna add too at our parameters where we will want obviously a personID as well as our street, suite, apartment, city, state, etc.


Pass DBService to AddEditAddressViewModel

And now we’re going to also go into our AddEditAddressViewModel. And we are going to pass this dbService to our ViewModel so we can save any addresses that edit with this ViewModel. And I’m gonna go ahead and creates a local reference to DBService. The next thing we want to do is update “doSave” method to not throw an exception because we don’t want our app to crash.


Add Init Method to AddEditAddressViewModel to Keep Track of Person to Add Address to

So next we’re gonna create an initialization function or at “Init” method that we’re gonna pass that the personID and will use this personID later in our “doSave” method. And next I’m going to update our “doSave” method to pass in the personId, street, etc. And then we’ll see we get this compiler error because we’ve forgot to pass in our DBService to use in our ViewModelLocator. And our last compile error is that we haven’t implemented the IDBService method for saving an address so let’s go ahead and do that.


Pass PersonId from Page to ViewModel

In our AddressPage, we need to update our constructor for AddressPage to take a personID. And what this will do is allow us to pass this to our ViewModels. So I’m going to go ahead and create a local field for our personID. And then I’m gonna override the “OnBindingContextChanged” method and whenever the view model that is assigned to this page changes we want to go ahead and call initialization function of our view model and pass it the personID. And now when we try to compile the app we here again have a compile error because we need to pass the clicked person ID to our address page.


Test Work Completed

And we’re gonna go ahead and set a breakpoint here just to make sure we’ve wired everything up correctly. And now we’ll see when we tap the add button our breakpoint catches and we can see that the person ID is getting passed correctly.


Implement SaveAddress Method in RealmDBService

So now I’m going to go into our RealmDBService and I’m gonna update our save address method to actually start saving addresses. So the first thing I wanna do is I want to get a reference to the person that we will be adding this address to. And then I’m going to instantiate a new address. And I’m also going to copy and paste our try catch block from our previous save function. And we want to do this because we’re going to create this new address in a transaction of sorts. And let’s finish up our try/catch here. Throw in our catch clause. Why retype when you can just cut and paste? And now we’re also gonna update all the properties of new address. And then lastly we’re gonna add this new address to our addresses RealmList.


Update PersonPage XAML to List Addresses

Before we run our application, we need to make sure that we have updated our addresses ListView and our PersonPage XAML to bind to that addresses collection in our view model. And I also need to go ahead and specify ItemTemplate for our ListView. And for this particular example I’m going to use what’s known as a TextCell which it kind of comes pre baked into Xamarin.Forms. And lastly we want to update our AddEditPersonViewModel to where it populates that addresses property with the addresses RealmList in our person model.


Implement Refresh Method for PersonPage and ViewModel

And then we’re also going to add a refresh method. So every time we come back to our screen we update addresses. And to do that will need to go into PersonPage, override the “OnAppearing” method and then make sure that we get a reference to our AddEditPersonViewModel and invoke the refresh method on that. Then we also need to add this null pointer checker.


Test and Enjoy

Select myself. Click “Add” for addresses. And now I can say something like this. Not how “Hell World”, but “Hello World.” City. St. Louis. Missouri. 63017. And we hit save. And come back well now see our addresses. So you can see with Realm Xamarin it is very easy to set up a one to many relationship between models.