Relationships in Realm

Henna Singh
3 min readFeb 15, 2022

I took a break from Flexbox and did some Realm today. I will not say this was easy but I have been trying to understand this topic for a long while, so I might as well spend some time on it.

If you are new to Realm, please feel free to check my “Getting Started with MongoDB Realm” series. Please feel free to ask any questions on our Community Forums

Relationships

Before you define a relationship in Realm, you first define a schema i.e your Realm Object Model. For Realm to recognize your classes, you create sub-classes of RealmObject.

What is a Relationship?

A relationship is when two objects are linked to each other. In Realm, relationships are direct references to other objects in Realm.

There are three primary types of relationships between objects:

One-to-One

When one Realm Object is related to not more than another Realm Object.

Example:

public class Book extends RealmObject {
public String name;
public Boolean isRead;
}

public class Person extends RealmObject {
public String name;
public Book book;
}

Person Oscar= realm.createObject(Person.class);
Oscar.name = "Oscar Newhart";

Book book1 = realm.createObject(Book.class);
book1.name = "Harry Potter and Deathly Hollows";

Oscar.book = book1;

Each Person has zero or one Bookinstance.

Setting the relationship field to null will clear the reference:

Oscar.book = null;

One-to-Many

You can create a relationship to any number of objects from a single object via a RealmList<T> field declaration. Let’s rewrite our example to support multiple email addresses:

public class Person extends RealmObject {
public String name;
public RealmList<Book> books;
}

public class Book extends RealmObject {
public String name;
public boolean isRead;
}

RealmLists are containers of RealmObjects; a RealmList behaves like a regular Java List. You can use the same object in different RealmLists, and you can use this to model both one-to-many and many-to-many relationships.

realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
Person person = realm.createObject(Person.class);
person.name = "John Doe";

Book book1 = realm.createObject(Book.class);
book1.name = "Lord of the Rings";
book1.isRead = true;
person.books.add(book1);

Book book2 = realm.createObject(Book.class);
book2.name = "Murakaami";
book2.isRead = false;
person.books.add(book2);
}
});

Inverse Relationships

Relationships are unidirectional.

public class Dog extends RealmObject {
private String name;
private int age;
}

public class Person extends RealmObject {
@PrimaryKey
private long id;
private String name;
private RealmList<Dog> dogs;
}

You can follow the link from a Person to a Dog, but there’s no way to go from a Dog to its Person objects. This is resolved by giving the Dog a @LinkingObjects annotation.

public class Person extends RealmObject {
private String id;
private String name;
private RealmList<Dog> dogs;
// getters and setters
}

public class Dog extends RealmObject {
private String id;
private String name;
private String color;
@LinkingObjects("dogs")
private final RealmResults<Person> owners;
// getters and setters
}

Realm Database automatically updates implicit relationships whenever an object is added or removed in the specified relationship. You cannot manually set the value of the inverse relationship property.

We’ve given Dog an owners field, and specified that it should contain all Person objects that have this Dog object in their dogs field.

Fields annotated with @LinkingObjects must be:

  • marked final
  • of type RealmResults<T> where T is the type at the opposite end of the relationship

Since relationships are many-to-one or many-to-many, following inverse relationships can result in zero, one, or many objects.

Summary

  • A relationship is an object property that allows an object to reference other objects of the same or another object type.
  • Relationships are direct references. You can access related objects directly through a relationship property without writing any type of join.
  • Realm Database supports to-one, to-many, and inverse relationships.

So, I did spend a lot of time reading about Relationships. For more details, also check on our Community Forums

--

--

Henna Singh

Technical Speaker and an aspiring writer with avid addiction to gadgets, meet-up groups, and paper crafts. Currently pursuing Full Stack Bootcamp from Nucamp