Say I’ve got a Cursor just returned from a database call:
Cursor myCursor = db.rawQuery(someQuery, null);
According to the docs, the Cursor isn’t actually populated before any calls are made to it, like getCount() for example. So my question is, does the following code actually query my database twice?
if(myCursor.getCount() > 1)
{
// Do something
}
else if(myCursor.getCount() == 1)
{
// Do something else
}
Or does Android cache the Cursor object after the first ‘if’ statement, making the ‘else if’ statement access the cached object instead?
No, it doesn’t. The cursor object actually contains it’s own count. You can see the source code for the SQLiteCursor object here.Take a look at the getCount() method:
It will only check the first time. mCount is only updated when the Cursor is requeried (see QueryThread.run() on the same page).