领域不保存(或可能不返回)相关对象的字符串值...
我有3个型号:
public class Customer extends RealmObject {
@Expose
@PrimaryKey
private Long id;
@Expose
private Long historicalId;
@Expose
private String versionUUID;
@Expose
private String nameCompany;
@Expose
private String email;
@Expose
private String phoneNumber;
@Expose
private String notes;
@Expose
private boolean active;
@Expose
private boolean currentVersion;
@Expose
private Date lastUpdated;
@Expose
private Date dateCreated;
public Customer() {
}
和
public class Project extends RealmObject {
@PrimaryKey
private Long id;
private Long historicalId;
private String versionUUID;
private String name;
private String description;
private String addressLineOne;
private String addressLineTwo;
private String addressCity;
private String addressState;
private String addressZip;
private String notes;
private Date lastUpdated;
private Date dateCreated;
private boolean active;
private boolean currentVersion;
private Customer customer;
private String customerVersion;
public Project() {
}
最后(为评论问题添加)
public class Receipt extends RealmObject {
@PrimaryKey
private String id;
private String name;
private String vendor;
private Double amount;
private String description;
private Date dateCreated;
private Date lastUpdated;
private Date dateSynced;
private byte[] imageByteArray;
private Project project;
private String projectVersion;
private int imgWidht;
private int imgHeight;
public Receipt() {
}
我通过以下方式保存数据:
public static void syncAllDataToRealm(Context context){
Globals globals = Globals.getInstance();
Realm realm = Realm.getInstance(context);
realm.beginTransaction();
realm.copyToRealmOrUpdate(globals.getAllCustomers());
realm.copyToRealmOrUpdate(globals.getAllProjects());
realm.commitTransaction();
testRealCommit(context);
}
我正在通过
验证数据 private static void testRealCommit(Context context){
Realm realm = Realm.getInstance(context);
RealmQuery<Customer> customerRealmQuery = realm.where(Customer.class);
RealmResults<Customer> customerRealmResults = customerRealmQuery.findAll();
logger.debug(LogUtility.generateMessage(TAG, "===== CUSTOMER ======= "));
for(Customer c: customerRealmResults){
logger.debug(LogUtility.generateMessage(TAG, c.getId() + " - " + c.getNameCompany()));
}
logger.debug(LogUtility.generateMessage(TAG, "===== CUSTOMER GLOBAL======= "));
for(Customer c: Globals.getInstance().getAllCustomers()){
logger.debug(LogUtility.generateMessage(TAG, c.getId() + " - " + c.getNameCompany()));
}
RealmQuery<Project> projectRealmQuery = realm.where(Project.class);
RealmResults<Project> projectRealmResults = projectRealmQuery.findAll();
logger.debug(LogUtility.generateMessage(TAG, "===== PROJECT ======="));
for(Project p: projectRealmResults){
logger.debug(LogUtility.generateMessage(TAG, p.getId() + " - " + p.getName()));
}
}
出于某种原因:
c.getNameCompany()
在上面的代码中返回一个null ...如果我不将项目数据添加到领域它工作正常....
域名是通过以下方式在我的应用程序文件中设置的:
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("receiptbucket.realm")
.schemaVersion(2)
.build();
Realm.setDefaultConfiguration(config);
任何想法???
发现其他东西......如果我交换提交订单,在添加所有项目后添加所有客户
realm.copyToRealmOrUpdate(globals.getAllProjects());
realm.copyToRealmOrUpdate(globals.getAllCustomers());
短期修复,但我想知道为什么我必须这样做,以便客户数据坚持......
(新发行)
现在当我为收据调用copyOrUpdate时,它会擦除所有客户数据项目客户....
答案 0 :(得分:1)
根据您的上一条说明,我认为问题是Project
返回的globals.getAllProjects()
列表包含Customer
null
nameCompany
的值Customer
。 / p>
原因是,当id
被调用,Realm将创建或更新时,@PrimaryKey
类的realm.copyToRealmOrUpdate(globals.getAllProjects())
注释为id
相关对象递归。 (这是更新的重点)。
如果找到的客户具有相同的Project.customer
且已经保存在Realm中,则只会使用Procject.customer.nameCompany
中的所有新值来更新Realm中存在的值。如果globals.getAllProjects()
为空,则会出现上述问题。
解决方案是让null
返回您想要更新的最新值,因为Realm无法理解{{1}}值是否是您想要忽略或更新的值。
答案 1 :(得分:0)
我能够通过查询领域并重新连接正在被收到项目的客户来克服最后一个错误,该项目是收据的一部分....
所以我开始玩....我从全局数据加载领域...当我从我的休息服务中获取数据时,我停止并加载了领域......
一旦这样做,一切都开始正常工作,大声笑......