Android ORMLite slowly creates an object

I use ormLite to store data on the device. I can’t understand why, but when I store about 100 objects, some of them store too much time, until the second. Here is the code

from DatabaseManager:

public class DatabaseManager public void addSomeObject(SomeObject object) { try { getHelper().getSomeObjectDao().create(object); } catch (SQLException e) { e.printStackTrace(); } } } public class DatabaseHelper extends OrmLiteSqliteOpenHelper public Dao<SomeObject, Integer> getSomeObjectDao() { if (null == someObjectDao) { try { someObjectDao = getDao(SomeObject.class); } catch (Exception e) { e.printStackTrace(); } } return someObjectDao; } 

Any ideas to avoid such situations?

+4
source share
1 answer

Thanks Gray! The solution, as mentioned, is Gray using the callBatchTasks method:

 public void updateListOfObjects (final List <Object> list) { try { getHelper().getObjectDao().callBatchTasks(new Callable<Object> (){ @Override public Object call() throws Exception { for (Object obj : list){ getHelper().getObjectDao().createOrUpdate(obj); } return null; } }); } catch (Exception e) { Log.d(TAG, "updateListOfObjects. Exception " + e.toString()); } } 

Using this method, my objects (two types of objects, type 1 - about 100 items, type 2 - about 150 items) are stored in 1.7 seconds.

See the ORMLite documentation.

+11
source

Source: https://habr.com/ru/post/1482710/


All Articles