gpt4 book ai didi

android - Realm 关系 : Many-to-One, 中的问题会创建重复对象

转载 作者:行者123 更新时间:2023-11-29 01:20:49 25 4
gpt4 key购买 nike

按照官方文档指南:Realm Relationships

我试着写了一些多对一关系的代码,发现有些地方不一致。

两个POJOContace.javaEmail.java定义如下:

public class Email extends RealmObject {
private String address;
private boolean active;
// ... setters and getters left out
}

public class Contact extends RealmObject {
private String name;
private Email email;
// ... setters and getters left out
}

情况一:创建一个普通的Email对象,并分配给不同的ContactcontactA和contactB。

Email email = new Email();
email.setAddress("realm_test@gmail.com");
email.setActive(true);

Contact contactA = new Contact();
contactA.setName("Bear");
contactA.setEmail(email);

Contact contactB = new Contact();
contactB.setName("Monkey");
contactB.setEmail(email);

realm.beginTransaction();
realm.copyToRealm(contactA);
realm.copyToRealm(contactB);
realm.commitTransaction();

在我调用realm.copyToRealm() 之后,将在此处创建两个Email 对象。那么,当我从 Realm 查询 Contact 时,一个 Email 对象会变成两个不同的对象吗?我认为这不再是多对一的关系,它只是变成了一对一的关系。 enter image description here

情况二:调用realm.createObject()创建一个代理Email对象和两个代理Contact对象,并将电子邮件分配给 contactA 和 contactB。

realm.beginTransaction();
Email email = realm.createObject(Email.class);
email.setAddress("realm_test@gmail.com");
email.setActive(true);

Contact contactA = realm.createObject(Contact.class);
contactA.setName("Bear");
contactA.setEmail(email);

Contact contactB = realm.createObject(Contact.class);
contactB.setName("Monkey");
contactB.setEmail(email);
realm.commitTransaction();

enter image description here在这里我们可以看到表中只有一个电子邮件对象,这正是我所期望的,正如上面文档中所描述的那样。

那么,为什么situation1situation2不一致呢?是situation1中的错误吗?还是我遗漏了什么?

Realm 版本:0.88.3

Android Studio 版本:2.0

期待您的回复!谢谢!

最佳答案

感谢beeender这里的答案:[issue-2730]

This is the expected behaviour.

In the situation1, the email you set to contactA and contactB is a standalone object which is not managed by Realm. So when you copy it to Realm, Realm has no way to know you mean they are the same object. To solve this, you can add a @PrimaryKey to Email and then use copyToRealmOrUpdate. Realm will try to detect if you mean the same email object for both contactA and contactB based on the primary key.

In the situation2, since the email object is managed by Realm, when you call setters, Realm knows that email is actually the same one.

最后我在ContactEmail中都设置了一个@PrimaryKey,如下:

public class Email extends RealmObject {
@PrimaryKey
private String address;
private boolean active;
// ... setters and getters left out
}

public class Contact extends RealmObject {
@PrimaryKey
private String name;
private Email email;
// ... setters and getters left out
}

然后将 contactA 和 contactB 上的 copyToRealm() 更改为 copyToRealmOrUpdate()

realm.beginTransaction();
realm.copyToRealmOrUpdate(contactA);
realm.copyToRealmOrUpdate(contactB);
realm.commitTransaction();

并且 ContactEmail 之间的关系在情况 1 中是正确的。

关于android - Realm 关系 : Many-to-One, 中的问题会创建重复对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36915559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com