Last active
June 2, 2017 18:01
-
-
Save Zhuinden/8c7b7c62f8df15255790685b3b6d4324 to your computer and use it in GitHub Desktop.
Realm backlinks 2: manual bi-directional links
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Dog extends RealmObject { | |
@PrimaryKey | |
private String id; | |
private int age; | |
private Person owner; // <--- !!! | |
// getters, setters | |
} | |
public class Person extends RealmObject { | |
@PrimaryKey | |
private String id; | |
private String name; | |
private RealmList<Dog> dogs; | |
// getters, setters | |
} | |
// creating objects | |
r.executeTransaction((realm) -> { | |
Dog dog = realm.createObject(Dog.class, "dogId"); | |
Person person = realm.createObject(Person.class, "personId"); | |
person.getDogs().add(dog); | |
dog.setOwner(person); // <--- !!! | |
}); | |
// link query | |
RealmResults<Dog> dogs = realm.where(Dog.class) | |
.equalTo("owner.name", "Jack") | |
.findAll(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment