androidsqlormliteandroid-cursor

CursorWindowAllocationException in standard ORMLite method


I need save some objects in DB. I'm using this code in my Dao class.

  public void saveActions(List<Action> actionList) throws SQLException {
    for (Action action : actionList) {
        createOrUpdate(action);
      }
  }

And sometimes I have CursorWindowAllocationException in createOrUpdate() function.

Does anyone have solution of this problem?


Solution

  • If you look up the source of CursorWindowAllocationException it reads:

    This exception is thrown when a CursorWindow couldn't be allocated, most probably due to memory not being available.

    If you follow the stack, you'll see that the call com.j256.ormlite.android.AndroidDatabaseConnection.queryForLong is creating a cursor for every createOrUpdate call.

    So what's likely happening here is that there are too many Cursors being created before the memory is freed.

    You should execute these calls in a transaction, or better yet, use batch tasks. E.g.

    actionDao.callBatchTasks(new Callable<Void>() {
            public Void call() throws SQLException {
                for (Action action : actionList) {
                    actionDao.createOrUpdate(action);
                }
            return null;
        }
    });