Update statement in Realm android

风格不统一 提交于 2019-12-03 05:48:11
j_gonfer

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 user 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();
                }
            });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!