Realm relation field always null [duplicate]

假如想象 提交于 2019-12-25 06:39:14

问题


There are my models:

public class RChat extends RealmObject {
    @PrimaryKey
    private String              Id;
    private RMyTest Test;

    public RChat() {}
}

and

public class RMyTest extends RealmObject {
    @PrimaryKey
    private String myName;

    public RMyTest() {
    }
}

And I'm using like this:

mRealm = Realm.getInstance(this);


        mRealm.beginTransaction();
        final RChat chat = mRealm.createObject(RChat.class);
        chat.setId("test");
        RMyTest rProfile = mRealm.createObject(RMyTest.class);
        rProfile.setMyName("alireza test");
        chat.setTest(rProfile);
        //mRealm.copyToRealmOrUpdate(chat);
        mRealm.commitTransaction();

        RChat chat1 = mRealm.where(RChat.class).equalTo("Id","test").findFirst();

but the chat1 object's Test field has null value always. How can I fix this problem?


回答1:


The code looks correct. If you get null by examine the chat1's Test field in the debug window, you will get a null value. That is expected.

Realm will generate a proxy class and override the getters/setters in the proxy class. So if you try

RMyTest rProfile = chat1.getTest();

I am sure you can get the corresponding RMyTest Object instead of null.

This behaviour is documented here.




回答2:


I was thinking my objects must not be null directly but the point is Realm uses proxy for models and the proxy is not null actually.



来源:https://stackoverflow.com/questions/32966955/realm-relation-field-always-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!