Android 中加载器相对于 Asynctask 有什么优势?
与异步任务相比,加载器有什么优势吗?另外,如何使加载程序与 Android froyo 手机兼容。
编辑:
这里的主要问题是我没有使用本机数据库(SqlLite)。使用开发服务器上的数据库。显然,我不能再使用 CursorLoader
了。 AsyncTaskLoader
根本没有示例。如果有的话,请做链接。
将所需数据加载到本地数据库然后使用 CursorLoader 进行查询是否是一个更好的主意?
Are there any advantages of Loaders over Async task? Also, how to make loaders compatible for phones with Android froyo.
Edit:
The primary problem here is that I'm not using the native DB(SqlLite). Using the DB on development server. Obviously, I can't use CursorLoader
any more. AsyncTaskLoader
has no examples at all. If any, please do link.
Is it a better idea to load the data required onto the local DB and then query it using CursorLoader
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,Loaders 比 AsyncTask 更有优势,因为它们可以处理 AsyncTask 所无法做到的很多事情。
在 AsyncTask 中更改屏幕方向很困难。我曾经遇到过这样的问题,直到我使用了 Activity Control 类,我在配置更改时保留了该类。如果您想知道如何操作,我可以给您一些代码。但是,当您甚至在加载整个数据之前多次更改方向时,该应用程序就会崩溃。这里的秘密是不要在第一个线程中加载大量数据,并尽快完成线程任务。即使它发生在后台,Android 处理线程的方式也很糟糕。您永远不知道您的任务之一何时会被终止。
即使您使用 AsyncTaskLoader,也要确保使用活动管理器。这将帮助您更好地控制 activites 和 AsyncTask。
是的,它兼容所有旧版本的Android。您需要包含支持库(大多数时候,默认情况下包含支持库,但仔细检查总是很好。)
Yes, Loaders are more advantageous than AsyncTask as they take care of a lot of things that AsyncTask falls short of, miserably.
Screen Orientation changes are difficult in AsyncTask. I used to have such a problem, till I used an Activity Control class, which I used to retain while configuration changed. I can give you some code if you want to know how. The app used to crash, though, when you changed the orientation multiples times even before the entire data loaded. The secret here is not load a lot of data with your first thread and and finish your threading tasks as soon as possible. Even if it happens in the background, Android has a shabby way of dealing with threads. You never know when one of your tasks would be killed.
Even if you use a AsyncTaskLoader, makes sure that you use an Activity manager. This will help you in getting more control over the activites and AsyncTask.
Yes, it is compatible in all the old version of Android. You need to include the support library(Most of the times, this is included by default but its always nice to double check.)
其一,加载器更容易编码(它们几乎内置在片段中)。
加载器(特别是 CursorLoader)还可以为您处理光标(如已弃用的 ManageQuery)。
查看此链接,了解如何使用 Honeycomb 之前的加载器。
For one, loaders are easier to code (they're almost built-in in Fragments).
Loaders (specifically CursorLoader) also handles your cursor for you (like the deprecated manageQuery).
Check out this link to read on how to use Loaders pre-Honeycomb.
更容易实现和处理许多生命周期管理,而这些管理以前必须使用 AsyncTasks“手动”完成。查看此问题的答案 了解更多详情。
关于将它们与 Froyo 一起使用,可以通过兼容性库获取它们。
There simpler to implement and take care of a lot of the life cycle management which previously had to be done "by hand" with AsyncTasks. See the answer to this question for further detail.
With regards to using them with Froyo, they're available via the compatibility library.
好像没有人谈论装载机的缺点!我目前正在开发一个在后台运行其他服务的系统。
我注意到的是,一旦带有加载程序的屏幕恢复。加载程序使用的游标锁定了数据库。
它可能不对大多数人开放,但 sqlite 中的 getDatabaseWriter 实际上是一个同步方法,因此加载器使用的游标永远不会关闭,直到加载器重置或终止,从而锁定对数据库的访问。
我不建议在这些情况下使用加载器,也不建议当结果集包含少于 100 个静态且似乎永远不会改变的项目时使用加载器。
It seems no one is talking about the disadvantages of loaders! I'm currently working on a system that runs other services in the background.
What I have noticed is that as soon as a screen with a loader is resumed. The cursor used by the loader locks up the DB.
It may not be open to most people but getDatabaseWriter from sqlite is actually a synchronized method and hence the cursor used by the loader is never closed until the loader is reset or terminated thus locking up access to the DB.
I cannot recommend using a loader under these circumstances nor can I advice using a loader when your result set consists of less than 100 items which are static and never seem to change.
加载器的另一个优点是它们可以优雅地处理屏幕转动事件,而 asynctask 可能会给您带来麻烦。
Another advantage with loaders is that they handle screen turn event gracefully whereas asynctask can give you trouble.
最大的区别:
一旦相关的 ContentProvider 更改其内容(例如通过服务),CursorLoader 就会更新 UI 的内容,而
AsyncTask 只会在您告诉它时更新您的 UI。
Biggest diff:
CursorLoader
will update your UI's content as soon as its relatedContentProvider
changes its content(say through aService
), whileAsyncTask
will only update your UI when you tell it.