简体   繁体   English

Android Loader是否在单独的线程中执行其工作?

[英]Does an Android Loader perform its work in a separate thread?

I am currently implementing my own Loader in Android, since I am using a library for loading stuff, that exposes its API only through callbacks and I still want to use the benefits of loaders. 我目前正在Android中实现自己的Loader ,因为我使用的是用于加载内容的库,该库仅通过回调公开其API,而我仍然想利用Loader的好处。

The library I want to use can take care of running asynchroneously itself or I can force it to run synchroneously and do the threading myself. 我要使用的库可以自己异步运行,也可以强制它同步运行并自己进行线程处理。 The Android documentation tells me the following: Android文档告诉我以下内容:

Introduced in Android 3.0, loaders make it easy to asynchronously load data in an activity or fragment. 加载程序是Android 3.0中引入的,可以轻松地在活动或片段中异步加载数据。 Loaders have these characteristics: 装载机具有以下特征:

... ...

  • They provide asynchronous loading of data. 它们提供数据的异步加载。

... ...

This I interpret as if a Loader always runs in its own Thread and performs the tasks asynchroneously. 我将其解释为好像Loader始终在其自己的线程中运行并异步执行任务。 However, if I then tell the library I want to perform its requests synchroneously, I get the following exception: 但是,如果我随后告诉该库我想同步执行其请求,则会收到以下异常:

AsyncHttpRequest: Unhandled exception origin cause
    android.os.NetworkOnMainThreadException
            at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)

So it seems as if the work I perform in my Loader runs on the Main thread after all, which surprises me. 因此,看来我在Loader执行的工作毕竟还是在Main线程上运行,这让我感到惊讶。 The code looks as follows: 该代码如下所示:

public abstract class AsyncHTTPLoader<E> extends Loader<E> {
    private Network2 mNetwork;
    private String mRequestUrl;

    private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            AsyncHTTPLoader.this.processResponse(new String(responseBody));
        }
    };

    public AsyncHTTPLoader(Context context, String url) {
        super(context);

        mRequestUrl = url;
        mNetwork = new Network2(getContext());
    }

    protected void processResponse(String response) {
        E result = parseResponse(response);
        deliverResult(result);
    }

    @Override
    protected void onForceLoad() {
        super.onForceLoad();
        Utils.log(mRequestUrl);
        mNetwork.get(mRequestUrl, mHandler);
    }

    public abstract E parseResponse(String response);
}

Do I need to take care of the threading in the loader myself or not? 我是否需要自己照顾加载程序中的线程? If I go with the Async feature of the library I use, does the processResponse() function block the UI Thread of my app? 如果我使用所用库的异步功能,那么processResponse()函数是否会阻止我的应用程序的UI线程?

您可以使用AsyncTaskLoader类为您创建一个backround线程。

This exception due to run Network operations on UI thread, if you want to use loader manager for network operations, you have to extend AsyncTaskLoader, and in the loadInBackground() do your network operations, in fact that this method called on a background thread. 此异常是由于在UI线程上运行网络操作引起的,如果要使用加载程序管理器进行网络操作,则必须扩展AsyncTaskLoader,并在loadInBackground()中执行网络操作,实际上,此方法在后台线程上调用。

for example: 例如:

MyLoader extends AsyncTaskLoader<List<MyItem>> {
    @Override
    public List<SampleItem> loadInBackground() {

       List<MyItem> itemsList = new ArrayList<MyItem>();

       // TODO: 

     return data;
    }
}

for more detailed example, you can refer to this link . 有关更详细的示例,您可以参考此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM