1

i have problem with get data from SQLite Cursor window allocation of 2048 kb failed using Thread background service

but if i run List branchList= DBTransaction.BranchList(); without Thread background service, this function is working normally

MyService.Java

public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i(TAG, "Service onStartCommand");
    new Thread(new Runnable() {
        @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
        @Override
        public void run() {
            while(true)
            {
                List<Branch> branchList= DBTransaction.BranchList();
                if (branchList.size() < 0)
                    getBranch(); //get branch using volley get data from web service

                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
    return START_STICKY;
}

SQLite GetData

public List<Branch> BranchList(){
    List<Branch> branchList = new ArrayList<Branch>();
    String selectQuery = "SELECT * FROM " + TABLE_BRANCH;

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()){
            do{
                Branch branch = new Branch();
                branch.setID(cursor.getString(0));
                branch.setName(cursor.getString(1));
                branch.setCode(cursor.getString(2));

                branchList.add(branch);
            }while (cursor.moveToNext());
        }
    }finally { 
         if(cursor != null)
         cursor.close();
    }
    return branchList;
}

Error result from android monitor

E/Volley: [10497] NetworkDispatcher.run: Unhandled exception android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=8 (# cursors opened by this proc=8)
                                                android.database.CursorWindowAllocationException: Cursor window allocation of 2048 kb failed. # Open Cursors=8 (# cursors opened by this proc=8)
rizkygum
  • 13
  • 1
  • 4
  • Possible duplicate of [SQLite Android Database Cursor window allocation of 2048 kb failed](https://stackoverflow.com/questions/11340257/sqlite-android-database-cursor-window-allocation-of-2048-kb-failed) – Abu Yousuf Feb 14 '18 at 02:32
  • i try solution from [link](https://stackoverflow.com/questions/11340257/sqlite-android-database-cursor-window-allocation-of-2048-kb-failed) and i get this code 'finally { if(cursor != null) cursor.close(); }' but the problem not solved – rizkygum Feb 14 '18 at 02:40

1 Answers1

0

"Could not allocate CursorWindow of size ### due to error ...."
occurs when a cursor is not closed and is called in loop.

if adding cursor.close(); in your code doesn't solve your problem,
apply strict rules as i have given in this answer here https://stackoverflow.com/a/65827816/2173890 and your will be pointed to the line, that is causing problem.

public List<Branch> BranchList(){
    List<Branch> branchList = new ArrayList<Branch>();
    String selectQuery = "SELECT * FROM " + TABLE_BRANCH;

    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = null;
    try {
        cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()){
            do{
                Branch branch = new Branch();
                branch.setID(cursor.getString(0));
                branch.setName(cursor.getString(1));
                branch.setCode(cursor.getString(2));

                branchList.add(branch);
            }while (cursor.moveToNext());
        }

cursor.close();//add this line as shown.

    }finally { 
         if(cursor != null)
         cursor.close();
    }
    return branchList;
}
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42