Reputation: 10474
I'm implementing my single logged in user through Realm. Every time an API call returns me a user object, I save the user into the DB using something like this:
realm.beginTransaction();
realm.createOrUpdateObjectFromJson(UserModel.class,jsonObject);
realm.commitTransaction();
I have a central function called loadUser()
which loads up the saved object like so:
private void loadUser(){
currentUser = realm.where(UserModel.class).findFirst();
}
Where currentUser
is obviously a member variable in my singleton.
This was working fine and dandy, until the point of time when I tried to refer to an object property. I'm pretty much using the RealmModel
implementing UserModel
class like a POJO but have wired up intelligent methods that return intelligent values for things like avatars, etc. I started noticing that the generated UserModelProxy
object does not have the values set when I inspect it in Debug mode, even though the database does, when I open it and browse it on OSX using RealmBrowser app.
if
conditions fail when I try to use the values directly.I just changed all my code to making the fields private
, and using them through generated getter and setter functions, which just directly accessing the variables. And those work!
Did I do something wrong, or does the realm java documentation fail to mention that I need to generate getter and setter functions that their generated Proxy classes will override?
Upvotes: 0
Views: 162
Reputation: 10474
I'm using the latest version of Realm (2.2.0) and after some more code debugging, it turns out that I was wrong, the problem was with my if condition, not with Realm database.
I guess it does make some sense to use getters/setters, though. Whatever magic Realm does with the generated proxy model classes, it doesn't translate through to inspection in the debugger. Knowing that we're supposed to use the getter and setter lets us get the correct values when we inspect in the Android Studio debugger also.
Upvotes: 1
Reputation: 3282
Yes, you should use accessors, because Realm uses native implementation and it proxies those values by those accessors. see this https://realm.io/docs/java/0.79.0/api/io/realm/RealmObject.html. "Annotation processor will create a proxy class for your RealmObject subclass. The getters and setters should not contain any custom code of logic as they are overridden as part of the annotation process."
Upvotes: 0