MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Using Backlink feature of realm-dotnet in Xamarin.Forms app

My current employer is developing a mobile app using Xamarin.Forms and Asp.net mvc on the backend. I suggested to use realm in the mobile app. My manager want to see a POC(Proof of concept) app using realm with backlink feature before allowing it to be used in the app. I am working on the POC on GitHub . The documentation is very limiting and the GitHub repo of realm-dotnet don’t have any good sample app.
I completed the project. But unable to implement backlink. The sample app I have developed allow user to create assignees(employees) in the first page. The user can delete or edit the employees using context menu. When the user clicks on the employee name the app navigates to the ToDoListPage of that particular employee. Here the user can create ToDoItems . On this ToDoList page I want to show the ToDoItems that where assigned to that employee only.
The models are as follows:

public class Assignee : RealmObject
{
public Assignee()
{
ToDoItems = Enumerable.Empty().AsQueryable();
}

    [PrimaryKey]
    public string Id { get; set; } = Guid.NewGuid().ToString();
    public string Name { get; set; }
    public string Role { get; set; }


    [Backlink(nameof(ToDoItem.Employee))]
    public IQueryable<ToDoItem> ToDoItems { get; }
}

public class ToDoItem : RealmObject
{
[PrimaryKey]
public string Id { get; set; } = Guid.NewGuid().ToString();
public string Name { get; set; }
public string Description { get; set; }
public bool Done { get; set; }

    public Assignee Employee { get; set; }
}

I am adding employee to each ToDo Item:

Item.Employee = Employee;
_realm.Add(Item);

Now I want to access the ToDoItems for the Employee:

Items = _realm.All<Assignee">().Where(x => x.Id == EmployeeId).FirstOrDefault().ToDoItems;

But this does not work. I will be grateful if someone can help me out by preferably writing code in my sample app or give the correct code in the reply.

Thank you

I didn’t get any reply on MongoDB community forum. But get the answer on Stackoverflow. So answering my own question. I am using realm 4.3.

Firstly, Realm .NET doesn’t currently support traversing properties (x.Employee.Id). Due to this the app crashes with the exception:

The left-hand side of the Equal operator must be a direct access to a persisted property in Realm

Realm supports object comparison, so we can fix this like so:

var employee = _realm.Find(EmployeeId);

Items = _realm.All().Where(x => x.Employee == employee);

The second issue is that the EmployeeId parameter is null. Since the EmployeeId is being populated after the load logic has been triggered, we don’t need to load the data in the ctor.

Finally, since I won’t be loading the data in the ctor, and instead in the SetValues method, the UI needs to know, when the data has been updated, what exactly to redraw. Thus, I need to mark the collection to be Reactive too:

[Reactive]

public IEnumerable Items { get; set; }

Then, I change the SetValues method to use object comparison, instead of traversing:

async Task SetValues()

{

Employee = _realm.Find(EmployeeId);

Title = Employee.Name;

Items = _realm.All().Where(x => x.Employee == Employee);

}

To sum up - I don’t need to try and load the data in the ctor, since I don’t know when the EmployeeId will be set. I already tracking when the property will change and inside the SetValues command simply need to change the expression predicate.

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