ContentProvider Threading
Asked Answered
J

3

6

Just curious.

getContentResolver().query(...)

I know that Loaders run queries on background threads. Does this also apply to inserts, updates and deletes? Should I create AsyncTasks, Threads, etc... for these kinds of calls? Large updates might block my application's main thread.

getContentResolver().insert(...)

Thanks!

Judges answered 6/3, 2012 at 8:35 Comment(0)
A
8

from Content Provider Basics

Retrieving Data from the Provider

This section describes how to retrieve data from a provider, using the User Dictionary Provider as an example.

For the sake of clarity, the code snippets in this section call ContentResolver.query() on the "UI thread"". In actual code, however, you should do queries asynchronously on a separate thread. One way to do this is to use the CursorLoader class, which is described in more detail in the Loaders guide. Also, the lines of code are snippets only; they don't show a complete application.

Aught answered 1/8, 2013 at 23:4 Comment(0)
R
3

The Loaders only works for queries, that is, loading data in Activities or Fragments, other than insert/update/delete.

Besides Loader, availabe since Android 3.0, android did provide a helper class AsyncQueryHandler, since API level 1, for async CRUD like 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. Using AsyncTask or simple worker threads are also popular practices in terms of particular requirements.

Reject answered 7/4, 2015 at 6:37 Comment(0)
O
1

When you say Loaders run queries on background threads, you need to understand that it is the implementations of the Loader class that perform the queries in the background thread.

The consequence of this is that you will want to use or extend and implement subclasses of the abstract Loader class and not use AsyncTask or Java threads. The reason is when you create a Loader, it expects you to return an instance of type Loader. A java thread or an AsyncTask cannot be used for this. Subclasses of the Loader class do this by starting their own thread. The client class or the class that creates the Loader has only initializes the Loader. The actual Loader instance can do anything you want it to do. This can be anything - insert, update, delete, etc. The Android provided CursorLoader implementation queries data from tables in a background thread asynchronously and returns the result set. Android does not provide similar native implementations for inserting or updating data. What you do have is the option to extend from the AsyncTaskLoader (the same class that CursorLoader inherits from).

Also, keep in mind that Loaders are used to optimize database access and as such will provide post the result to the main thread only when they have completed their task and not before that (there is a hack like workaround to post updates to the UI thread but then this breaks the paradigm). Loaders are only concerned with completing their work and are not interested in providing any updates.

If you simply want to handle ContentProvider queries asynchronously, you might want to consider using the AsyncQueryHandler class supplied by Android.

Oho answered 7/4, 2015 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.