Note: In order to understand this blog post, you will first need to check out Gabor Varadi’s great post on Realm backlinks and linking objects. This post is meant to be a follow up to it (or if you already have knowledge of backlinks and linking objects within Realm).

As shown in Gabor’s example, backlinks are a powerful feature within Realm whenever you want to return a child object while querying a field from the parent object.

Wouldn’t it be great if we could return a child object while querying the parent and even the grandparent object to filter out results coming back in the RealmResult object?

I wasn’t sure if it was possible given how I couldn’t find an example where it was shown and after doing some testing, I realised that this was in fact possible to do with the power of Realm.

Below is the football schema I have produced for the purpose of this demo. It’s a somewhat simplified model of the real football world.

Relationship summary: Each league has a list of clubs and every club has a manager.

Setting up the backlinks

Now you are ready to filter the League and Club fields to return a Manager when performing a RealmQuery.

Examples

I’ve posted a couple example scenarios below showing what queries you can perform now you have the backlinks setup within Realm.

If you are looking to test these examples feel free to download the example I have within my GitHub profile: https://github.com/MarkOSullivan94/RealmBackLinkExample

Scenario 1: Find the manager of a specific club.

Shown below is how you would go about searching for the Manager who is manager of Chelsea and managing in the Premier League.

Scenario 2: Find all of the managers who are currently in a certain league.

Conclusion

That was a short example showing how you can easily set up multi-level backlinks using linked objects even when using a fairly simple schema. They are equally as effective in a much bigger more complex schema with even more levels of backlinks.

I hope you too find a way to use this useful feature!