Adnan Khan
Adnan Khan

Reputation: 493

How to make or implement self relationship in realm database?

I have a table called User. User have friends.

public class User extends RealmObject {
    @PrimaryKey
    private long id;
    private String name;
    private int age;
    private String email; 
}

I am using the User class to add User and its friends to realm. My question is, how to link user to its friends?

Upvotes: 2

Views: 712

Answers (1)

Adnan Khan
Adnan Khan

Reputation: 493

I just looked through the realm documentation and found my question answer there.

It is possible to declare recursive relationships which can be useful when >modelling certain types of data.

public class Person extends RealmObject {
    private String name;
    private RealmList<Person> friends;
    // Other fields…
}

Use recursive relationships with care as Realm does not currently have cycle detection and you can easily end in infinite loops.

Setting the value to null for a RealmList field will clear the list. That is, the list will be empty (length zero), but no objects have been deleted. The getter for a RealmList will never return null. The returned object is always a list but the length might be zero.

Upvotes: 1

Related Questions