Android Realm Handling Primary Key in Relational Object

橙三吉。 提交于 2019-12-08 03:10:17

问题


I have two objects: MailBox and Email. Each Receiver has many Emails.

public class MailBoxRealmModel extends RealmObject {

    @PrimaryKey
    private long id;

    private String name;
    private String mailboxId;
    private RealmList<EmailRealmModel> emails;
}

public class EmailRealmModel extends RealmObject {

    @PrimaryKey
    private long EmailId;

    private String Name;
    private String Text;
    private String Tag;
    private int Type;
    private String Time;
    private int Status;
}

How can one use **realm.insertOrUpdate**when adding email to MailBoxRealmModel?

EmailRealmModel email = new EmailRealmModel();
email.setMessageId(emailID);

realm.insertOrUpdate(email );

mailBoxRealmModel.getEmails().add(email);

I am getting a null pointer exception email on this line:

mailBoxRealmModel.getEmails().add(email);

Exception:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean io.realm.RealmList.add(io.realm.RealmModel)' on a null object reference at com.koa.mailbox.MailBoxActivity$1.execute(MailBoxActivity.java:123) at io.realm.Realm.executeTransaction(Realm.java:1253) at com.koa.mailbox.MailBoxActivity.test(MailBoxActivity.java:88) at com.koa.mailbox.MailBoxActivity.onCreate(MailBoxActivity.java:71) at android.app.Activity.performCreate(Activity.java:6876) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)  at android.app.ActivityThread.access$1100(ActivityThread.java:222)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7229)  at java.lang.reflect.Method.invoke(Native Method)


回答1:


Proper way to create a new RealmObject with existing primary key is usage realm.createObject(EmailRealmModel.class, emailID)method.

Full code fragment:

realm.beginTransaction();
EmailRealmModel email = realm.createObject(EmailRealmModel.class, emailID);
mailBoxRealmModel.getEmails().add(email);
realm.commitTransaction();

Or, if you wish update stored in realm object using one of inmemory instances, you should call realm.copyToRealmOrUpdate(obj).

Example from documentation:

// For create managed proxy, you should wrap EmailRealmModel object by call copyToRealmXXX
EmailRealmModel email = realm.copyToRealmOrUpdate(new EmailRealmModel(id));
mailBoxRealmModel.getEmails().add(email);



回答2:


On an unmanaged RealmObject, the RealmList fields must be initialized manually.

EmailRealmModel email = new EmailRealmModel();
email.setMessageId(emailID);

mailBoxRealmModel.setEmails(new RealmList<MailBoxRealmModel>());
mailBoxRealmModel.getEmails().add(email);
realm.insertOrUpdate(mailBoxRealmModel);


来源:https://stackoverflow.com/questions/41366560/android-realm-handling-primary-key-in-relational-object

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