我正在使用 Realm 将我的值存储在本地数据库中。
我的要求是我需要根据某些条件更改一个字段status=1
。
我尝试了以下方法来完成此任务。它运行良好。
RealmResults<NotificationOrder> notificationOrders=realm
.where(NotificationOrder.class)
.equalTo(RealmConstants.TBL_NOTIFICATION_ORDER.property_id,ConstantMethod.getPreference(getActivity(),UserDefault.kPropertyId))
.equalTo(RealmConstants.TBL_NOTIFICATION_ORDER.status,0)
.findAll();
for (NotificationOrder order:notificationOrders) {
realm.beginTransaction();
order.setStatus(1);
realm.commitTransaction();
}
现在我的本地数据库中可能有 1000 行这样的行,使用 for 循环更新单行似乎不是正确的方法。
所以我的问题是:有没有像 MYSQL
Update Queries in Realm 这样的方法,我们可以通过单个语句更新状态为 0 的所有行,而不是逐行更新?
谢谢。
最佳答案
如果我没记错的话,事务中的对象应该是可管理的,所以
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmResults<NotificationOrder> notificationOrders = realm
.where(NotificationOrder.class)
.equalTo(RealmConstants.TBL_NOTIFICATION_ORDER.property_id,ConstantMethod.getPreference(getActivity(),UserDefault.kPropertyId))
.equalTo(RealmConstants.TBL_NOTIFICATION_ORDER.status,0)
.findAll();
for(NotificationOrder order : notificationOrders) {
order.setStatus(1);
}
}
});
应该足够了。
关于android - 在 Android 中使用 Realm 更新表中的多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38782287/