Android ORMLite slow create object

被刻印的时光 ゝ 提交于 2019-12-30 09:55:15

问题


I am using ormLite to store data on device. I can not understand why but when I store about 100 objects some of them stores too long time, up to 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 this situations?


回答1:


Thanks to Gray! Solution is, as mentioned Gray, using 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 way, my objects (two types of objects, 1st type - about 100 items, 2nd type - about 150 items) store in 1.7 sec.

See the ORMLite documentation.



来源:https://stackoverflow.com/questions/16749209/android-ormlite-slow-create-object

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