I have some questions regarding Cursor
.
I want to know what are the disadvantages of not closing a Cursor
?
It deallocates resources, but what resources are deallocated?
If we do not close the Cursor
? What will be the consequences and to what extent?
Not closing a Cursor
does not affect Activity
a bit, but it gives error in a log cat.
Yes, it's recommended to close the cursor when you are done using that cursor object so that cursor can do whatever house keeping work it wants to do upon closure.
Not closing a cursor will keep locks active that it holds on the rows where it is positioned.
To close the cursor, call the Cursor. close() method. Note that if you close a database that has cursors open in it, then it will throw an exception and close any open cursors for you. For best results, close your cursors from within a finally block.
Closing a Cursor
will avoid any potential memory leaks, so YES they should always be closed when no longer used.
I don't think you'll see many issues from forgetting to close one cursor. The issue (and this applies to almost any memory leak) is that if you keep doing it over and over again, eventually something bad will happen.
For example, I wrote an app that uploads data from a device's SQLite DB to a server. If I didn't close the cursor each time I was done reading and writing from the database, eventually I would run into out-of-memory errors and various other issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With