Update mutiple rows in table using Realm in Android

怎甘沉沦 提交于 2019-12-14 01:10:51

问题


I am using Realm to store my values in local database.

My requirement is that i need to change one field status=1 based on some condition.

I have tried following method to accomplish this task. And it is working fine.

   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();
    }

Now there may be 1000 of such rows in my local db and using for loop to update single row doesn't seem proper way.

So my question :Is there any way like MYSQL Update Queries in Realm by which we can update all rows having status=0 by single statement instead of updating single row one by one ?

Thanks.


回答1:


If I know right, the objects in the transaction ought to be managed, so

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);
        }
    }
});

Should be sufficient.



来源:https://stackoverflow.com/questions/38782287/update-mutiple-rows-in-table-using-realm-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!