正在使用的AsyncTask仍建议在后台加载的ListView项目?(Is using AsyncT

2019-08-17 16:45发布

Background

I've heard that there are some new solutions for loading data in the background which are more recommended than AsyncTask (like loaders).

The problem

AsyncTasks are great and easy to use. However, it has some limitations:

  1. The class itself has to be modified since it's limited by the number of pending tasks (about 256 or so). Of course, in a listView's adapter, I always cancel a task if it's not needed(for example when I need to update a view that was used for a different item).

  2. I also have to cancel them all (or handle in a different way) when the activity/fragment is being re-created.

  3. Because of 1&2, I need to manage them and have a reference to all of them

  4. AsyncTask uses a queue of tasks, and sometimes I need to use a stack instead, so I had to create my own class of AsyncTask that uses a stack instead.

The question

Are there alternatives for AsyncTask?

I know this was asked in some posts before (like here), but I was thinking if there is a new general way to load data in the background which replaces the asyncTask.

About Loaders, I think the idea is that they are used for databases and contentProviders, but can they also be used for loading (for example) data from the Internet (like images files) ?

There is also a nice sample made by google (here, called "bitmapFun"), which according to what I see uses AsyncTask (and even extend it, maybe because of the same reasons I've mentionsed) . But maybe I'm missing there something too?

Answer 1:

也许你应该考虑检讨你的方法,你必须执行,具体取决于视图几次更新,并取消从以前的视图的所有尚未完成的任务需要给您的每一个需要创建的视图单独执行数据的负载印象。

在与列表适配器的列表视图,通常的办法是加载数据的一部分(作为的ValueObject的或作为从多个数据库中的行光标列表)分页按需或者在一个目标,而不是由项项目。 所以,如果你要更新的下一个页面,你基本上执行一个单一的操作,或者使用的AsyncTask或装载机,以获取新的项目模型,然后使其可用于用户界面来显示它们。 这样,您将申请MVC,你会不会有一些尚未完成的任务,取消和控制,你的结构会更扎实,更易于管理。

关于替代品,如果你正在处理数据库,最简单的方法是使用CursorLoader,即装载机替代的AsyncTask ,但如果你正在处理的来自网络或文件系统的数据,你还挺免费从各种可用的其他选项中进行选择。 的AsyncTask是更易于使用,大多推荐简单的事情,或一次性查询。 但你也可以使用装载机这样的任务,以及,看到AsyncTaskLoader 。



Answer 2:

是。

装载机是管理AsyncTasks。 如果您使用的不是Loader,则很可能缺少他们需要的管理。

AsyncTasks(和装载机)是一个非常糟糕的方式来获得的东西,是关闭设备。 要获得从远程服务器看看数据导入使用IntentService。 请参阅: https://www.youtube.com/watch?v=xHXn3Kg2IQE



Answer 3:

的AsyncTask的设计是围绕线程和处理程序一个辅助类,并不构成通用线程框架。 AsyncTasks应该理想地(最多几秒钟。)可用于短期操作。如果您需要保持对长时间运行的线程,强烈建议您使用介绍java.util.concurrent pacakge如提供的各种API遗嘱执行人,并ThreadPoolExecutor的FutureTask。 见http://developer.android.com/reference/android/os/AsyncTask.html更多信息。

到的AsyncTask一种替代方法是robospice。 https://github.com/octo-online/robospice 。

您可以在这里开始robopice。 https://github.com/octo-online/robospice/wiki/Starter-Guide 。

在robospice的样品https://play.google.com/store/apps/details?id=com.octo.android.robospice.motivations&feature=search_result 。

一些robospice的特点。

1.executes asynchronously (in a background AndroidService) network requests (ex: REST requests using Spring Android).

2.is strongly typed ! You make your requests using POJOs and you get POJOs as request results.

3.enforce no constraints neither on POJOs used for requests nor on Activity classes you use in your projects.

4.caches results (in Json with both Jackson and Gson, or Xml, or flat text files, or binary files, even using ORM Lite).

5.notifies your activities (or any other context) of the result of the network request if and only if they are still alive

6.no memory leak at all, like Android Loaders, unlike Android AsyncTasks notifies your activities on their UI Thread.

7.uses a simple but robust exception handling model.


文章来源: Is using AsyncTask still recommended for loading listView items in the background?