Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'RealmNoticeModelClass' has a primary key, use 'createObject(Class<E>, Object)' instead

After Upgrading Realm from 1.1.0 to 2.0.2. I get this error while transacting object. In earlier version it was working fine.

io.realm.exceptions.RealmException: 'RealmNoticeModelClass' has a primary key, use 'createObject(Class, Object)' instead.

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)

Code is

 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
}
like image 461
Sahaj Rana Avatar asked Oct 18 '16 07:10

Sahaj Rana


1 Answers

Primary keys are immutable since Realm 2.0.0, which means you cannot modify them after the object has been created.

Considering you have a primary key, the object should be created with its primary key value set.

In your case, this primary key is

@PrimaryKey
private String timeStamp;

This means, this line of code will fail:

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

And it should be

RealmNoticeModelClass realmNoticeModelClass = 
   realm.createObject(RealmNoticeModelClass.class, snapshotKey /* primaryKeyValue */);
like image 68
EpicPandaForce Avatar answered Oct 27 '22 00:10

EpicPandaForce