0

I am using the room database in my application. I have a feature to download some content within my application. When each content is saved in my DB, I need to update a table that has columns that indicate whether each content is saved or not. We set a boolean in the table when content is saved.

So the flow is: Call an API, save its contents, and after saving update column in another table to true. This happens for multiple APIs. Each content save success will have the update query executed in another table. All API calls are called sequentially.

I am getting the following error for the DAO.

Caused by android.database.CursorWindowAllocationException Cursor window allocation of 2048 kb failed. # Open Cursors=321 (# cursors opened by this proc=321)

When each content is saved, code flow is as follows:

myDao.updateStatus(id) //updates a column value to true
    .subscribeOn(Schedulers.io())
    .subscribe {
        myDao.getContent(id) //Select query to check id is present in table or not
             .subscribeOn(Schedulers.newThread())
             .subscribe { count ->
                  if (count > 0) {
                       //Call next API  
                  }
        }
    }

The DAO is just updating a small content and selecting a single row that has only a few boolean columns. So, this doesn't come up to 2MB. Then why is this exception occurring in this DAO?

Anju
  • 9,379
  • 14
  • 55
  • 94
  • Does this answer your question? [android.database.CursorWindowAllocationException when moving a Cursor](https://stackoverflow.com/questions/21219039/android-database-cursorwindowallocationexception-when-moving-a-cursor) – Rahul Rawat Jun 25 '21 at 07:16

1 Answers1

0

Refer here. It seems you are calling the same method in which al this is written from where you have written call next api which is preventing previous cursors from closing and essentially putting you in a recursive loop.

Try using

Single.fromCallable {
   //synchronous api call here
   //database insertion of content here
   //database updation of status here
   //put all the above in a loop
}
.subscribeOn(Schedulers.io())

This way your code will become much more readable and understandable. But if you still prefer to call api asynchronously try using an intermediate layer to handle the connection between the network layer and the database layer like a view model and first fetch network result on that and on the success callback add subscribe to the aforementioned code.

Rahul Rawat
  • 360
  • 2
  • 9