Realm android中的更新语句 [英] Update statement in Realm android
本文介绍了Realm android中的更新语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我应该如何在Android中使用Realm DB更新现有值?
How should i update a already existing value using realm DB in android?
我一直在尝试更新它,但是它只是作为新值添加而不会覆盖它
I have been trying to update it but it is adding as a new value only not overwritting it
推荐答案
在Realm DB中使用其所有字段更新现有对象的另一种方法是使用方法realm.copyToRealmOrUpdate()
:
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 :)
更多信息: copyToRealmOrUpdate( )
这篇关于Realm android中的更新语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文