DBFlow - update multiple items efficiently

893 Views Asked by At

ArrayList list =... --> Get data from database

//do some changes in the values of the objects in the list

for(MyObject object:list){ object.save(); }

Instead of this for loop is there a way to save multiple items that is more efficient than calling save() a bunch of times?

2

There are 2 best solutions below

0
On BEST ANSWER

Finaly found it

FlowManager.getModelAdapter(MyTable.class).saveAll(myObjectsList);

and it's faster than calling save multiple times. From some quick testing for 15 items the multiple save took an average of 120 milliseconds while saveAll took about 100 milliseconds. percentage wise it's quite a big difference.

2
On

Calling save() multiple times is ok. If there is a thousand entries, you can do it with asynchronous transaction to avoid blocking UI.

If you really want to do save one time, create a new table(model) name ListObject, then use the relationship one-to-many to store it as a List<Object>. So have to save one time only. https://agrosner.gitbooks.io/dbflow/content/Relationships.html