ContentProvider Threading

Just curious.

getContentResolver().query(...) 

I know that Loaders run requests in the background thread. Does this also apply to inserts, updates, and deletes? Should I create AsyncTasks, Threads, etc. For these challenges? Large updates can block the main thread of the application.

 getContentResolver().insert(...) 

Thanks!

+4
source share
3 answers

from Content Provider Basics

Retrieving data from a provider

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

For clarity, the code snippets in this section are called by ContentResolver.query () in the "user interface thread". In real code, however, you must make requests asynchronously in a separate thread . One way to do this is to use the CursorLoader class, which is described in more detail in the forklift manual. In addition, lines of code are snippets only; they do not show the full application.

+5
source

Loaders work only for requests, i.e. load data into actions or fragments, except insert / update / delete.

In addition to Loader, available with Android 3.0, the Android did provide an AsyncQueryHandler helper class, since API level 1 is for asynchronous CRUD operations with support for all CRUD callbacks.

AsyncQueryHandler works internally with HandlerThread for async operations and returns the results to the main thread. Using AsyncTask or simple workflows is also a popular practice in terms of specific requirements.

+1
source

When you say that Loaders run requests in the background thread, you need to understand what exactly the Loader class implementations execute requests in the background thread.

The consequence of this is that you will want to use or extend and implement subclasses of the Loader abstract class, rather than using AsyncTask or Java streams. The reason is that when you create a Loader, it expects you to return an instance of type Loader. You cannot use java or AsyncTask thread for this. Subclasses of the Loader class do this by creating their own thread. The client class or the class creating the Loader initializes the loader only. The actual instance of Loader can do whatever you want. It can be anything - insert, update, delete, etc. Android has implemented the implementation of CursorLoader queries from tables in the background thread asynchronously and returns a result set. Android does not provide similar built-in implementations for inserting or updating data. You have an extension option from AsyncTaskLoader (the same class that CursorLoader inherits).

Also, keep in mind that Loaders are used to optimize access to the database and, as such, will send the result to the main thread only when they have completed their task, and not earlier (there is a way to hack, for example, a workaround for publishing updates the user interface stream, but then this violates the paradigm). Loaders are engaged only in the completion of their work and are not interested in providing any updates.

If you just want to handle ContentProvider requests asynchronously, you may need to use the AsyncQueryHandler class provided by Android.

+1
source

Source: https://habr.com/ru/post/1399951/


All Articles