I have a custom content provider and I need to insert one record at a time when user clicks on some button, and it is not too often. I was wondering that should I do this insert in background thread (AsynchTask, etc) or should I just insert it on GUI thread? Right after insertion I have to update GUI.
If I should do it in background thread then what is the best approach... I guess asynctask is the best choice.
Thanks in advance :)
-
1The only reason I would not suggest using asynctasks is that it adds a LOT more extra concerns. For instance, your activity shouldn't finish until all async tasks have finished. Also, you should think about what will happen if the user clicks the button rapidly. Content providers aren't thread safe, so you would have to do synchronization yourself. It's best practice to use an asynctask, but it's up to you to decide if it's really necessary.Samuel– Samuel2012-02-14 20:27:21 +00:00Commented Feb 14, 2012 at 20:27
Add a comment
|
2 Answers
I advise against accessing your provider on the main thread. Use CursorLoader for queries. Use ContentProviderOperation objects and ContentResolver.applyBatch() for modifications. These classes handle all of the background work for you.
ASyncTask is a much more general-purpose class.
1 Comment
eddy
Some examples on how to use ContentProviderOperation and ContentResolver.applyBatch() would have been great