Update statement in Realm android

Another way to update an existing object with all its fields in your Realm DB is using the method realm.copyToRealmOrUpdate():

Object obj = new Object();
obj.setField1(field1);
obj.setField2(field2);
realm.beginTransaction();
realm.copyToRealmOrUpdate(obj);
realm.commitTransaction();

If your object has a Primary Key, this method will update the object automatically without duplicate objects :)

More info: copyToRealmOrUpdate()


You can use insertOrUpdate method to do this.Hope this helps

  Realm.getDefaultInstance().executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {

                    objectToEdit.setNewValue("string");
                    realm.insertOrUpdate();
                }
            });

Tags:

Android

Realm