0

My code have some sqlite operators. I execute a sql, it has 500 rows. Executing and getting rows take quite low time, but when I call cursor.close() function, it takes 10 seconds.

How can I reduce this time consume ?

Cursor cursor = database.rawQuery(sql, null);
while(cursor.moveToNext()){
     String str1 = cursor.getString(0);
     String str2 = cursor.getString(1);
     System.out.println(str1+" and "+str2);
}
cursor.close();  // this line takes 13 seconds. but above code takes only 200 ms

code is something like this

2
  • instead of closing cursor have you tried closing database object??
    – user370305
    Aug 24, 2012 at 9:14
  • no, I dont try. but, I need database object for next queries.
    – Adem
    Aug 24, 2012 at 9:14

1 Answer 1

1

I have suggest you to use method startManagingCursor(Cursor c) of Activity Class. Lets Activity handle Cursor for you as per Activity Life Cycle..

Form Docs StartManagingCursor...

public void startManagingCursor (Cursor c)
Since: API Level 1

This method is deprecated. Use the new CursorLoader class with LoaderManager instead; this is also available on older platforms through the Android compatibility package.

This method allows the activity to take care of managing the given Cursor's lifecycle for you based on the activity's lifecycle. That is, when the activity is stopped it will automatically call deactivate() on the given Cursor, and when it is later restarted it will call requery() for you. When the activity is destroyed, all managed Cursors will be closed automatically.

If you are targeting HONEYCOMB or later, consider instead using LoaderManager instead, available via getLoaderManager().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.