Android - SQLite ContentResolver insert/delete/update on UI Thread? Android - SQLite ContentResolver insert/delete/update on UI Thread? sqlite sqlite

Android - SQLite ContentResolver insert/delete/update on UI Thread?


I also got confused about the samples making calls on the main thread. I guess the samples just simplified the demonstrations avoiding extra threads and callbacks, since single insert/update/delete call may return quickly.

Besides the Loader pattern for query, android did provide a helper class AsyncQueryHandler, since API level 1, for async CRUD operations with full CRUD callbacks supported. The AsyncQueryHandler works inside with a HandlerThread for the async operations and delivers the results back to the main thread.

So I do believe the ContentProvider queries should run in worker threads other than the UI, and those samples may not be best practices according to the official design.

=== edit

Found an annotation from the official framework docs, see this or this, Line 255:

In practice, this should be done in an asynchronous thread instead ofon the main thread. For more discussion, see Loaders. If you are notjust reading data but modifying it, see {@link android.content.AsyncQueryHandler}.

=== edit 2Link to actual android dev guide containing the above quote


This question has been on my mind since a long time. I guess, this depends on the complexity of the file we are trying to Insert, Update or Delete. If our application is going to Insert or Update large files, it would be always right to do it asynchronously and if the files aren't going to be that big, running it on UI thread can be done.

However, it is always recommended to continue with Database operations on a separate thread.


I think you've answered your own question. I do believe CursorLoader extends AsyncTaskLoader. Calls made from UI thread only process the call TO the CusorLoader (which uses AsyncTask.) What is being done BY the call still does not occur on UI Thread. Making a call to a method/function that then runs things on a seperate thread is still doing work away from UI thread.

What work do you think is happening on the UI thread?

Please show Debug log if possible or example where you think work is done on UI.It shouldn't be.

Not trying to argue just want to know how you've come to the conclusion of UI work?