在片段中使用指南AsyncTaskLoader和的AsyncTask中进行选择 [英] Guideline to choose among AsyncTaskLoader and AsyncTask to be used in Fragment

查看:188
本文介绍了在片段中使用指南AsyncTaskLoader和的AsyncTask中进行选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/example/android/supportv4/app/LoaderCustomSupport.java">LoaderCustomSupport (使用AsyncTaskLoader)和<一href="http://grep$c$c.com/file/repository.grep$c$c.com/java/ext/com.google.android/android-apps/4.0.1_r1/com/example/android/supportv4/app/FragmentRetainInstanceSupport.java">FragmentRetainInstanceSupport (使用线程,几乎等同于AsyncTask的)

Look at LoaderCustomSupport (Use AsyncTaskLoader) and FragmentRetainInstanceSupport (Use Thread, almost equivalent to AsyncTask)

这两个例子具有以下共同点。

Both examples have the following similarities.

  • 在加载数据时既不会阻塞UI线程
  • 在数据读取时,用户执行像屏幕旋转配置更改线程不被破坏。
  • 当数据读取线程读取完数据时,它可以更新到正确的片段UI

然而,也有不同之处。

  • 好像有没有简单的方法来更新到进度条对话框中间的进步
  • 在不知道这一点。但是Android的文件似乎建议<一href="http://developer.android.com/reference/android/content/AsyncTaskLoader.html">AsyncTaskLoader对于异步数据加载和更新的最终结果UI

有没有指引,或清单来看待,做出决定是否要选择AsyncTaskLoader或AsyncTask的,做一个耗时加载任务和更新结果片段的UI?

Is there any guideline, or checklist to look at, to make a decision on whether to choose AsyncTaskLoader or AsyncTask, to do a time-consuming loading task and update the result to Fragment's UI?

推荐答案

你的问题让我感兴趣,并试图有时寻找到的差异。在这里,我写我的观察。

your question made me interested and tried sometimes to look into the differences. Here i am writing my observations.

  1. 对于premature终止使用的AsyncTask将继续在它的线程中运行的异步任务。结果的处理可能很快导致未请求的结果,而AsyncTaskLoader处理活动的premature终止

  1. For the premature termination the asynchronous task using AsyncTask will continue running in its thread. The processing of the results can soon lead to unrequested results while AsyncTaskLoader handle the premature termination of the activity

AsyncTaskLoader处理活动的配置变化(即当用户旋转屏幕)。

AsyncTaskLoader handles activity configuration changes (IE when the user rotates the screen).

AsyncTaskLoader适用于DataAdapters加载数据,以便为达到此目的,最好使用AsyncTaskLoader但是如果你需要任务完成后更改用户界面(特别是碎片),最好是使用AsyncTask的,你不能改变的片段在AsynTaskLoader的onLoadFinished。

AsyncTaskLoader is intended load data for DataAdapters so for this purpose it is best to use AsyncTaskLoader But if you need to change UI (specially fragments) after task completion it is better to use AsyncTask as you can't change fragments in onLoadFinished of AsynTaskLoader.

所以我的使用取决于你的任务。而如果上述3点犯规打扰你的性能将相同(都没有发现任何文件,虽然,但在这种情况下asynctaskloader建议:S)

So to me the usage depends on your task. and if the above 3 points doesnt bother you then the performance is same ( haven't found any documents though , but in this case asynctaskloader is recommended :S)

一些相关链接

AsyncTaskLoader VS AsyncTask的

<一个href="http://andreas-kluck.blogspot.com/2012/02/asynctask-and-asynctaskloader.html">http://andreas-kluck.blogspot.com/2012/02/asynctask-and-asynctaskloader.html

这篇关于在片段中使用指南AsyncTaskLoader和的AsyncTask中进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆