Search code examples
androidandroid-contentprovider

Should calls to a Content Resolver be done in a Service (i.e separate thread)?


I'm learning more about content providers in Android and they're performance. For example, if I write my own content provider (subclass ContentProvider) for SQLite operations and then I want to do an operation in an Activity's onPause() method, then in my Activity I would do:

@Override
protected void onPause (){
    super.onPause();

    // ...prepare myTransactionValues

    // This could be anything like insert, bulkInsert, query, delete, etc.
    getContentResolver().insert(CONTENT_URI, myTransactionValues)
}

However, it looks likes this database operation is being done on the main thread. Is the work being done in a separate thread behind the scenes? Should it be in it's own thread? If it should, is there a recommended way (perhaps a Service)?


Solution

  • If you want to do something in a Non-UI thread, you should usually use an AsyncTask. A service, on the other hand, is best kept for long tasks.