空对象中的领域查找查询结果 [英] Realm Find Queries Result in Empty Objects

查看:87
本文介绍了空对象中的领域查找查询结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查找对象的查询时,我得到的是空"对象(非null,但未填充).但是,在调试器中,我可以在对象描述中看到该对象的数据(请参见下文).我还使用Realm Browser验证了数据是否存在.我尝试了不同的查找查询,使用过滤条件进行查询,对插入/查询使用相同的Realm对象,对插入/查询使用不同的Realm对象,刷新了Realm,等等.

When doing find queries for objects I'm getting "empty" objects (non-null, but not populated). However, in the debugger I can see the data for the object in the object description (see below). I've also verified the data is there using the Realm Browser. I've tried different find queries, querying with filter criteria, using the same Realm object for inserts/queries, using different Realm objects for inserts/queries, refreshing the Realm, etc.

如果我在RealmObject中记录字段,我会看到正确的数据打印出来.但是,我正在尝试根据 https:将这些模型转换为RxJava中使用的其他模型://realm.io/news/using-realm-with-rxjava/.

If I Log fields in the RealmObject I see the proper data print out. However, I'm trying to convert these models into other models for use in RxJava per https://realm.io/news/using-realm-with-rxjava/.

以下是一些示例代码,其中再现了该问题.下面是在verifyRealm.close()处断开时的屏幕截图.

Here's some sample code where reproduced the issue. Below that is a screenshot when breaking at verifyRealm.close().

RealmTester realmTester1 = new RealmTester();
realmTester1.setFirstName("Tester1");
realmTester1.setLastName("ABC");
RealmTester realmTester2 = new RealmTester();
realmTester2.setFirstName("Tester2");
realmTester2.setLastName("XYZ");

Realm insertRealm = Realm.getDefaultInstance();
insertRealm.refresh();
insertRealm.beginTransaction();
insertRealm.copyToRealm(realmTester1);
insertRealm.copyToRealm(realmTester2);
insertRealm.commitTransaction();
insertRealm.close();

Realm verifyRealm = Realm.getDefaultInstance();
RealmResults<RealmTester> verifyTesters = verifyRealm.where(RealmTester.class).findAll();
verifyRealm.close();

我在以下位置有调试器的屏幕截图: http://i.stack.imgur.com/1UdRr.png

I have a screenshot of the debugger at: http://i.stack.imgur.com/1UdRr.png

我正在使用v0.82.1.关于为什么这里的模型没有填充的任何想法?

I'm using v0.82.1. Any thoughts on why the models here aren't populating?

推荐答案

realm-java背后的想法是,我们生成的Proxy类继承自用户的模型类,并在那里覆盖了setter和getter.

The idea behind realm-java is that we are generating Proxy class inherits from user's model class, and override the setters and getters there.

由于在Realm中未设置模型字段的值,因此在调试器中看到null值是完全正常的. (零拷贝,Realm试图通过管理本机代码中的数据并在可能的情况下共享它们来减少内存使用.)

It is totally normal that you see null values for the model's field in the debugger, since the Realm are not setting them. (zero-copy, Realm is trying to reduce the memory usage by managing the data in the native code and sharing them whenever it is possible.)

因此,当您要访问Realm模型的字段时,请始终使用setter和getter.检查生成的Proxy类将帮助您理解这一点,实际上这很简单.它位于名为MyModelRealmProxy.java

Because of this, when you want to access a Realm model's field, please always use setters and getters. Checking the generated Proxy class will help you to understand this, it is quite simple actually. It is located in the build directory named like MyModelRealmProxy.java

还要检查文档的部分,它会给您关于standalone对象以及如何将它们写入Realm的一些想法.

And also check this section of the documents, it would give you some idea about the standalone object and how to write them to Realm.

这篇关于空对象中的领域查找查询结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆