Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm Find Queries Result in Empty Objects [duplicate]

Tags:

android

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.

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/.

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();

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

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

like image 324
dpalmer Avatar asked Sep 27 '22 22:09

dpalmer


1 Answers

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.

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.)

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

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.

like image 101
beeender Avatar answered Sep 30 '22 07:09

beeender