Simple use:
Cursor cursor = db.query(...); while (cursor.moveToNext()) { ... }
moveToFirst is used when you need to start an iteration from the beginning after you have already reached a position.
Avoid using cursor.getCount () unless necessary. And never use the getCount () loop.
getCount is expensive - it iterates through many records to count them. It does not return a stored variable. There may be some caching on the second call, but the first call does not know the answer until it is counted.
If your query matches 1000 lines, the cursor actually only has the first line. Each moveToNext performs a search and finds the next match. getCount should find all 1000. Why iterate over everything if you need only 10? Why repeat twice?
In addition, if your query does not use an index, getCount can be even slower - getCount can go through 10,000 records, even if the query matches only 100. Why is the cycle 20,000 instead of 10,000?
Pla
source share