为什么使用,而不是简单的处理器AsyncTaskLoader与LoaderManager,?处理器、而不是、简单、LoaderManager

2023-09-04 09:11:38 作者:Struggle(挣扎)

关闭UI线程,然后修改用户界面运行的异步任务是在Android开发一个共同的问题,所以我决定休息一段时间,研究和玩不同的技术,并找到最适合我的。

Running asynchronous tasks off of the UI thread then modifying the UI is a common issue in android development, so I decided to take some time, research, and play around with different techniques and find what works best for me.

我考虑的重要因素:

应工作可靠 code可读性 活动片段应保持清洁尽可能多的线程管理,尽可能的 Should work reliably Code readability Activity or Fragment should be kept clean of as much of thread management as possible

下面是我的即时通讯pressions有关的各种方法的概要(其可能是错误的,有些是只是意见):

Here is the summary of my impressions (which may be wrong and some are just opinions) about the various methods:

我是用简单的的AsyncTask 没有 LoaderManager 当我第一次跳入Android的:

I was using simple AsyncTask without LoaderManager when I first jumped into Android:

有间歇性的问题,我写我自己 AsyncTaskManager 与活动的生命周期管理。 有一些限制,以任务和内存泄漏的数量已经被报道过。 最大的问题,这是他们让我的code非常令人费解,并简化了code击败了使用它们摆在首位的目的。 Had intermittent issues, I wrote my own AsyncTaskManager to manage them with the activity life cycle. There are some limitations to number of tasks and memory leaks have been reported before. Biggest issue with these was that they made my code extremely convoluted, and simplifying the code defeated the purpose of using them in the first place.

这似乎是在建议办法做到这一点,所以我研究了一下:

This seems to be the recommended way to do this, so I researched it a bit:

在阅读这些了一下之后,似乎这种方法建议的主要理由是因为它管理与片段生命周期的任务,并从我的理解基本上只如果有必要重新启动任务。这似乎并不能够接收任务的结果开始的活动已重新启动活动后,重新启动之前。 所有任务参数似乎是 Parcelable Serialiazable 进入一个对象。 After reading about these a bit, it seems the main reason this method is recommended is because it manages the tasks with the Fragment life cycle, and from my understanding basically just restarts the tasks if necessary. It doesn't seem to be able to receive the results of a task started before an activity was restarted after the activity restarts. All the task parameters seem to have to be Parcelable or Serialiazable to go into a Bundle object.

这是我看中的方法:

易于实施,非常个性化。 您可以访问执行任务的线程:设置优先级,进行调试设置线程的名称,设置守护进程等 似乎比使用AsyncTasks的基础上,一个眼睛测试,我点击一个按钮很多次,看结果和线程闪过更为敏感;)我所能基准本。 要处理生命周期问题,可以编写管理信息(仍然存在,而这个过程是活的)一个单例类。将它们存储在一个给定的活动的处理程序没有设置,然后将它们转发到活动的处理程序,如果它要求其错过的消息。意一个任务不必重新使用相同的参数,其可以是对那些非幂等任务至关重要。

我到了,使用处理程序主题的消息是一个更好的解决办法,但我相信,我失去了一些东西,因为几乎到处都是我看着的建议是使用 AsyncTaskLoader 方法。我在想什么?

So I came to the conclusion that using Handler, Threads, and Messages is a much better solution, but I'm convinced I'm missing something because nearly everywhere I looked the recommendation was to use the AsyncTaskLoader method. What am I missing?

感谢您的输入。

推荐答案

什么你缺少的是类,如的AsyncTask LoaderManager 写了Android的初衷。意,操作系统被设计相比,台式计算机,以使大多数最小的硬件。 的AsyncTask 限制了你的线程池,因为你必须比其他系统更严格的线程限制。如果您尝试产卵100多线程,新主题将被拒绝或系统崩溃。你当然可以使用处理程序,但你对你自己的,只要对其进行管理。

What you're missing is that classes like AsyncTask and LoaderManager were written with Android in mind. Meaning, the OS is designed to make the most of minimal hardware when compared to a desktop computer. AsyncTask limits your thread pool because you have much stricter thread limitations than on other systems. If you try to spawn 100+ threads, new threads will be rejected or crash the system. You can certainly use Thread and Handler, but you're on your own as far as managing it.

最近我听说,的AsyncTask 支持10个线程与10个任务队列深度(它可能在以后的版本中增加)。如果这是有限制的,你总能抓住源头,写你自己的。我以前已经做到了。你要考虑的重要一点是,我遇到了麻烦来与生成过多线程的道路,如果是的话,我应该如何去处理它。

Last I heard, AsyncTask supports 10 threads with a queue depth of 10 tasks (it may have increased in later versions). If this is restrictive, you can always grab the source and write your own. I've done it before. The important thing you want to consider is, can I run into trouble down the road with spawning too many threads, and if so, how am I going to handle it.

要解决你的问题,为什么它推荐使用 LoaderManager AsyncTaskLoader ,它只是一个方便。这是一个简单的方法来重新载入数据,并把它给你的code依赖于该数据的部分。它不保证在各种情况下。

To address your question as to why it's recommended to use LoaderManager and AsyncTaskLoader, it's just a convenience. It's an easy way to reload data and get it to the parts of your code that depend on that data. It's not warranted in every situation.