Android的ORMLite慢创建对象对象、Android、ORMLite

2023-09-07 12:43:01 作者:风居住の街道

我使用ormLite存储设备上的数据。 我不明白为什么,但是当我存储大约100个​​对象他们中的一些商店太长的时间,上升到第二。 这里是code

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

从的DatabaseManager:

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?

推荐答案

感谢格雷! 解决方案是,如前所述灰色,使用callBatchTasks方式:

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

在1.7秒店内​​

Using this way, my objects (two types of objects, 1st type - about 100 items, 2nd type - about 150 items) store in 1.7 sec.

使用这种方式,我的对象(约150项两种类型的对象,第一类 - - 约100个​​项目,第二类)。

请参见 ORMLite文档。

See the ORMLite documentation.