1

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 :)

1
  • 1
    The 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. Commented Feb 14, 2012 at 20:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Some examples on how to use ContentProviderOperation and ContentResolver.applyBatch() would have been great
0

Ya you are right .. Using Async Task for managing DB Operations is a good approach. But the decision to choose particular approach rests on the amount of data you are querying or inserting from / to database.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.