&#39; RealmNoticeModelClass&#39;有一个主键,使用&#39; createObject(Class <e>,Object)&#39;代替

时间:2016-10-18 07:22:08

标签: java android realm

将Realm从1.1.0升级到2.0.2后。我在处理对象时遇到此错误。在早期版本中,它工作正常。

io.realm.exceptions.RealmException:&#39; RealmNoticeModelClass&#39;有一个主键,使用&#39; createObject(Class,Object)&#39;相反。

E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: com.rana.sahaj.myyu, PID: 20202
                                                                 io.realm.exceptions.RealmException: 'RealmNoticeModelClass' has a primary key, use 'createObject(Class<E>, Object)' instead.
                                                                     at io.realm.Realm.createObjectInternal(Realm.java:821)
                                                                     at io.realm.Realm.createObject(Realm.java:802)
                                                                     at com.rana.sahaj.myyu.notice.NoticeOnItemSelectedActivity$1$2.onSuccess(NoticeOnItemSelectedActivity.java:162)
                                                                     at com.rana.sahaj.myyu.notice.NoticeOnItemSelectedActivity$1$2.onSuccess(NoticeOnItemSelectedActivity.java:156)
                                                                     at com.google.android.gms.tasks.zze$1.run(Unknown Source)
                                                                     at android.os.Handler.handleCallback(Handler.java:733)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:136)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:5052)
                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                     at java.lang.reflect.Method.invoke(Method.java:515)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                                                                     at dalvik.system.NativeStart.main(Native Method)

代码是

 realm.beginTransaction();
                    RealmNoticeModelClass realmNoticeModelClass = realm.createObject(RealmNoticeModelClass.class);
                    realmNoticeModelClass.setDocUrl(DocUrl1);
                    realmNoticeModelClass.setHashname(heading);
                    realmNoticeModelClass.setContent(contentString);
                    realmNoticeModelClass.setTime(getIntent().getExtras().getString("notice_time"));
                    realmNoticeModelClass.setPicurl(picUrlName);
                    realmNoticeModelClass.setUserPic(getIntent().getExtras().getString("image_user_notice"));
                    realmNoticeModelClass.setPdfUrl(PdfUrl);
                    realmNoticeModelClass.setPicOffline(SnapshotKey);
                    realmNoticeModelClass.setMetaType(null);
                    realmNoticeModelClass.setTimeStamp(SnapshotKey);
                    realm.commitTransaction();

n RealmNoticeModelClass

public class RealmNoticeModelClass extends RealmObject {
@PrimaryKey
private String timeStamp;
private String Content;
@Index
private String UserPic;
private String Hashname;
private String picurl;
private String time;
private String picOffline;
private String pdfUrl;
private String docUrl1;
private String metaType;

public String getMetaType() {
    return metaType;
}

public void setMetaType(String metaType) {
    this.metaType = metaType;
}

public String getDocUrl() {
    return docUrl1;
}
 ... other getters setters
}

1 个答案:

答案 0 :(得分:33)

自Realm 2.0.0以来,主键是不可变的,这意味着在创建对象后无法修改它们。

考虑到您有一个主键,应该创建该对象,并设置其主键值。

在您的情况下,此主键是

@PrimaryKey
private String timeStamp;

这意味着,这行代码将失败:

RealmNoticeModelClass realmNoticeModelClass = 
   realm.createObject(RealmNoticeModelClass.class);
realmNoticeModelClass.setTimeStamp(snapshotKey /* primaryKeyValue*/);

它应该是

RealmNoticeModelClass realmNoticeModelClass = 
   realm.createObject(RealmNoticeModelClass.class, snapshotKey /* primaryKeyValue */);