I can't find a way to do an update using Realm in Android Java .
Adding data and deleting no problem, I only have problems modifying existing data, it always jumps out to me that it primary key
already exists and cannot be overwritten, but it is obvious that I assign it the same id in order to overlap the changes.
My RealmInfoDB object is a realm table
import io.realm.RealmObject;
import io.realm.annotations.PrimaryKey;
public class RealmInfoDB extends RealmObject {
@PrimaryKey
private long id;
private long lastUpdate;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(long lastUpdate) {
if (lastUpdate == 0) lastUpdate = System.currentTimeMillis();
this.lastUpdate = lastUpdate;
}
}
To add a new record
realm.beginTransaction();
RealmInfoDB infoDB = realm.createObject(RealmInfoDB.class);
infoDB.setId(0);
infoDB.setLastUpdate(0);
realm.commitTransaction();
To modify I use the same and that is when I get the error
realm.beginTransaction();
RealmInfoDB infoDB = realm.createObject(RealmInfoDB.class);
infoDB.setId(0);
infoDB.setLastUpdate(0);
realm.commitTransaction();
Mistake
io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists: 0
integer error log
06-09 00:24:01.909 E/AndroidRuntime: FATAL EXCEPTION: main
Process: realm.test.app.testrealm, PID: 1177
java.lang.RuntimeException: Unable to start activity ComponentInfo{realm.test.app.testrealm/realm.test.app.testrealm.MainActivity}: io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists: 0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4032)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists: 0
at io.realm.internal.Table.throwDuplicatePrimaryKeyException(Table.java:684)
at io.realm.internal.Table.addEmptyRow(Table.java:371)
at io.realm.Realm.createObject(Realm.java:696)
at realm.test.app.testrealm.MainActivity.onCreate(MainActivity.java:41)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4032)
at android.app.ActivityThread.access$900(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1323)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
@Webserveis id is defined as primary key, updating this field would not be possible:
Try this way where it would only update
lastUpdate
:The error is that you are generating 2 inserts with the same ID.. you should do this
On the other hand, I note that in the first query it would be necessary to add
copyToRealm(infoDB);
Well, an error should have appeared here if you hadn't added
copyToRealm
it, I don't know why you didn't get an error, but well...