No encuentro la forma para hacer un update usando Realm en Java de Android.
Agregar datos y eliminar ningún problema, solo tengo problemas en modificar datos ya existentes, siempre me salta que la primary key
ya existe y no se puede sobrescribir, pero es que es obvio le asigno el mismo id para así solapar los cambios.
Mi objeto RealmInfoDB Es una tabla realm
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;
}
}
Para agregar un nuevo registro
realm.beginTransaction();
RealmInfoDB infoDB = realm.createObject(RealmInfoDB.class);
infoDB.setId(0);
infoDB.setLastUpdate(0);
realm.commitTransaction();
Para modificar uso lo mismo y es cuando me salta el error
realm.beginTransaction();
RealmInfoDB infoDB = realm.createObject(RealmInfoDB.class);
infoDB.setId(0);
infoDB.setLastUpdate(0);
realm.commitTransaction();
Error
io.realm.exceptions.RealmPrimaryKeyConstraintException: Value already exists: 0
Log error entero
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)