Data not being added to the Realm Database

I am developing a project using MongoDB realm. But get stuck in the beginning of the project. I am adding a model to the Realm Database. But due to some reason it does not work. The code where I am adding the model is as follows:

public MilkManProfile MilkManProfile { get; set; }
 realm = Realm.GetInstance();
 MilkManProfile = realm.All<MilkManProfile>().FirstOrDefault();

 public async Task Login()
        {
            bool isCodeValid = await ValidateVerificationCode();
            if (isCodeValid)
            {         
                realm.Write(()=>
                {                 
                        realm.Add(MilkManProfile);              
                });       
                await AppShell.Current.GoToAsync("milkManProfile");
            }
        }

But the Model is not being added. The Model class is:

 public class MilkManProfile : RealmObject
    {
        [PrimaryKey]
        [MapTo("_id")]
        public ObjectId UserId { get; set; } = ObjectId.GenerateNewId();
        public string Name { get; set; }
        public DateTimeOffset DateOfBirth { get; set; }
        public string Sex { get; set; }
        public string Locality { get; set; }
        public string Pin { get; set; }
        public string PostOffice { get; set; }
        public string District { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string PhoneNumber { get; set; }

    }

When I try to access the MilkManProfile in another ViewModel it returns null. Here is the link to the Project

Looking at your code, it doesn’t appear you’re ever creating a MilkManProfile - you look it up by calling realm.All<MilkManProfile>().FirstOrDefault(), but that will return null if there are no objects in the Realm. You need to handle that case and add a new object like:

MilkManProfile = realm.All<MilkManProfile>().FirstOrDefault();
if (MilkManProfile == null)
{
    MilkManProfile = realm.Write(() =>
    {
        var profile = new MilkManProfile();
        // set properties if necessary
        return realm.Add(profile);
    });
}

Thank you for the prompt reply. This worked but I with a little quirk. I was binding the Entry with MilkManProfile.PhoneNumber . But this does not persist phone number. Instead I need to bind it with ViewModel property name PhoneNumber. Than I need to write the transaction like this to persist the Phone Number.

realm.Write(()=>
{
MilkManProfile.PhoneNumber=PhoneNumber;
realm.Add(MilkManProfile);
});

@nirinchev It is lot’s of increased code and complexity for defining properties to bind with the View. The UI binding with Realm Models is not working. Please resolve the issue sooner so that I can move ahead with my project.

What exactly is the issue? The problem above was a bug in the code you sent us and as far as I understand, you’ve been able to fix it following my comment.

Yes I able to fix the problem following your comment. But now I have another problem as per my last comment. Should I start new topic for the problem of Realm Model not binding to the UI.

What do you mean that it’s not binding to the UI? How did you try to bind it and what is not working?

I was binding the Entry with MilkManProfile.PhoneNumber in the LoginViewModel . But this does not update MilkManProfile.PhoneNumber when user input the number in the view. Instead I need to bind it with PhoneNumber property of the LoginViewModel. Than I need to write the transaction like this to persist the Phone Number.

realm.Write(()=>
{
MilkManProfile.PhoneNumber=PhoneNumber;
realm.Add(MilkManProfile);
});

It is lot’s of increased code and complexity for defining properties to bind with the View as binding with Realm Model does not work. I have to assign each ViewModel property to the Realm model property in the Write transaction.
You can see the MilkManProfileViewModel where I have define about 10 properties to bind with the View just because Binding the View with MilkManProfile Realm Model does not work.
Hope i able to explain the issue clearly.

How are you doing the databinding? If it’s the same way as in the project that you attached in the first comment, i.e.

new Label{}.Bind(Label.TextProperty,nameof(vm.MilkManProfile.PhoneNumber))

Then this is wrong because nameof(vm.MilkManProfile.PhoneNumber) will return "PhoneNumber". Instead, you need $"{nameof(vm.MilkManProfile)}.{nameof(vm.MilkManProfile.PhoneNumber)}" which will return the correct binding path - i.e. "MilkManProfile.PhoneNumber".

The issue is that if you try to bind to properties on MilkManProfile, the databinding engine assumes those are properties on the view model itself, which is why you need to supply the complete path.

1 Like

This will save lots of extra effort. Thank you

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.