Realm (Java) database using the document (Relationships)


Realm (Java) database using the document (directory)

You can use any two RealmObject linked together. In the Realm of the relationship is very light: traverse speed links in terms of memory or does not take up too many resources. Let's explore Realm allows you to define different types of relationships between objects.

Many-5.1

To create "many" relationship or "one to one", please give a model attribute whose type is one of your RealmObject subclasses:

public class Email extends RealmObject {
    private String address;
    private boolean active;
}

public class Contact extends RealmObject {
    private String name;
    private Email email;
}

Contact bob = realm.createObject(Contact.class);
bob.name = "Bob Newhart";

Email email1 = realm.createObject(Email.class);
email1.address = "[email protected]";

bob.email = email1;

Each contact has zero or one instance of e-mail. Nothing can stop you will use the same e-mail objects with multiple contacts; the difference between the many-to-one relationship and the relationship depends on your business logic.

The relationship field set to clear the reference to null:

bob.email = null;

This will remove the relationship between the bob and email1, but email1 still in the Realm.

5.2-many

You can RealmList <T>create a relationship with any number of objects from a single object fields statement. Let's rewrite the example to support multiple e-mail addresses:

public class Contact extends RealmObject {
    public String name;
    public RealmList<Email> emails;
}

public class Email extends RealmObject {
    public String address;
    public boolean active;
}

RealmLists is RealmObjects container; RealmList behave like regular Java List. You can use the same object in different RealmLists and can use the object on the "many" and "many to many" relationship modeling.

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

        Email email1 = realm.createObject(Email.class);
        email1.address = "[email protected]";
        email1.active = true;
        contact.emails.add(email1);

        Email email2 = realm.createObject(Email.class);
        email2.address = "[email protected]";
        email2.active = false;
        contact.emails.add(email2);
    }
});

It can declare a recursive relationship, which is useful when certain types of data modeling.

public class Person extends RealmObject {
    public String name;
    public RealmList<Person> friends;
    // 其他字段...
}

Set the value RealmList field to null to clear the list. List will be empty (zero length), but the list does not remove the object from the Realm. RealmList-getter will never return null: the object is returned is always a list. Length may be zero.

5.3 inverse relationship

Relationship is unidirectional. Person and Dog with these two classes, for example:

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 click on a link from the "people" to "dog", but not from the "dog" to their "people" object. You can resolve this problem by providing @LinkingObjects annotated as Dog.

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
}

We offer an owner field Dog, and specify that the field should contain all Dog Person object with this object in its field dogs.

Annotated field must be declared final, and must be RealmResults <T>the type, wherein T is the end opposite relationship type / class. Since the relationship is many-to-many or, therefore follow the inverse relationship may result in zero, one or more objects.

Like any other RealmResultsset the same, you can query an inverse relationship.

5.4 primitive list (Lists Of Primitives)

Realm Model class can contain a list of primitive data types. It must be used RealmList <T>for modeling, where T may be the following types: String, Integer, Boolean, Float, Double, Short, Long, Byte, byte[]and Date.

public class Person extends RealmObject {
  public String name;
  public RealmList<String> children = new RealmList<>();
}

And RealmModela different list, a list of primitives may contain a null value. If you do not allow null values, use the @Requiredannotation:

public class Person extends RealmObject {
  public String name;
  @Required
  public RealmList<String> children = new RealmList<>();
}

Primitive list does not support list and a list of queries.

Before Realm Java 4.0.0, typically use a specific Realm <String / Int>class modeling primitives list. You can use the following method to migrate to the migration code from primitive list:

// Model classes
public class RealmString extends RealmObject {
  public String value;
}

public class Person extends RealmObject {
  public String name;
  @Required
  public RealmList<String> children = new RealmList<>();
}

// Migration code
RealmObjectSchema objSchema = realmSchema.get("Person");
objSchema.addRealmListField("children_tmp", String.class)
        .setRequired("children_tmp", true)
        .transform(new RealmObjectSchema.Function() {
            @Override
            public void apply(DynamicRealmObject obj) {
                RealmList<DynamicRealmObject> children = obj.getList("children");
                RealmList<String> migratedChildren = obj.getList("children_tmp", String.class);
                for (DynamicRealmObject child : children) {
                    migratedChildren.add(child.getString("value"));
                }
            }
        })
        .removeField("children")
        .renameField("children_tmp", "children");
Published 59 original articles · won praise 88 · views 190 000 +

Guess you like

Origin blog.csdn.net/geofferysun/article/details/105102526