Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ORMLite slow create object

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?

like image 399
Aleksandr Avatar asked Dec 20 '22 05:12

Aleksandr


1 Answers

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.

like image 85
Aleksandr Avatar answered Dec 30 '22 11:12

Aleksandr