Cursor fetching performance in non UI thread

84 views
Skip to first unread message

andrew

unread,
Jan 5, 2012, 1:27:09 AM1/5/12
to android-platform
I've got a performance problem while fetching data from a cursor in
AsyncTaskLoader. Testing in android API level 10. For example 2
classes with cursor - TestFragmentUI fetching data in UI thread and
TestFragment fetching data in none UI thread.

public class TestFragment extends Fragment implements
LoaderManager.LoaderCallbacks<Object> {
...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
...
getLoaderManager().initLoader(0, null, this);
...
}

public android.support.v4.content.Loader<Object> onCreateLoader(
int id, Bundle args) {
return new Loader(getActivity());
}

class Loader extends AsyncTaskLoader<Object> {


public Loader(Context context) {
super(context);
}
public Object loadInBackground() {
...
DataBaseHelper helper = new DataBaseHelper(getContext());
SQLiteDatabase database = helper.getReadableDatabase();
long start = System.currentTimeMillis();
Cursor data = database.rawQuery(String.format(SQL_LOAD,
parametr), null);
while (data.moveToNext()) {
String number =
data.getString(data.getColumnIndex("number"));
}
data.close();
Log.i(TAG, "load: " + (System.currentTimeMillis() - start));
...
}
}
...
}

and

public class TestFragmentUI extends Fragment {
...

@Override
public void onActivityCreated(Bundle savedInstanceState) {
...
DataBaseHelper helper = new DataBaseHelper(getActivity());
SQLiteDatabase database = helper.getReadableDatabase();
...
long start = System.currentTimeMillis();
Cursor data = database.rawQuery(String.format(SQL_LOAD, parametr),
null);
while (data.moveToNext()) {
String number = data.getString(data.getColumnIndex("number"));
}
data.close();
Log.i(TAG, "load: " + (System.currentTimeMillis() - start));
...
}

...
}
In emulator TestFragment class output time that is 10 times slower
than output TestFragmentUI and visually it is very noticeable. On a
real device (I've got a DHD) 3-fold difference - still very big.

Any ideas?

Christopher Tate

unread,
Jan 5, 2012, 3:28:04 PM1/5/12
to android-...@googlegroups.com
You're running the operation in an AsyncTask, which by default runs at background priority.  That's why it takes much longer than when you run it at normal priority.  Try calling Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT) within your AsyncTaskLoader implementation.  This will raise that thread's priority to normal, so it should get more of the CPU to perform the work in question.

Obviously this will potentially affect foreground UI responsiveness while the operation is running; you'll need to balance that against time-to-completion.

--
christopher tate
android framework engineer



--
You received this message because you are subscribed to the Google Groups "android-platform" group.
To post to this group, send email to android-...@googlegroups.com.
To unsubscribe from this group, send email to android-platfo...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-platform?hl=en.


andrew

unread,
Jan 6, 2012, 1:42:45 AM1/6/12
to android-platform
Thx a lot.
Reply all
Reply to author
Forward
0 new messages