什么是CursorLoaders的好处是什么? [英] What are the benefits of CursorLoaders?

查看:110
本文介绍了什么是CursorLoaders的好处是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用光标广泛我的应用程序,加载和偶尔和数据库写入信息。我已经看到,蜂窝和兼容包有新的旨在帮助装载数据的好办法装载机类。

I use Cursors extensively in my app, to load and occasionally write information from and to a database. I have seen that Honeycomb and the Compatibility Package have new Loader classes designed to help with loading data in a "good" way.

从本质上讲,这些新类(特别是 CursorLoader )要比管理数据previous方法更好?什么是造福一个 CursorLoader 通过托管光标例如?

Essentially, are these new classes (in particular CursorLoader) considerably better than previous methods of managing data? What is the benefit of a CursorLoader over managed Cursors for example?

和我用的是的ContentProvider 来处理数据,这显然需要尤里斯但如何做到这一点目与 initLoader()的方法?我必须建立我的每个片段用装载机分别?而如何唯一没有的ID必须为每个装载机,是它在我的应用程序的范围,或只是一个片段?有没有简单的传递乌里来一个CursorLoader查询我的数据?

And I use a ContentProvider to deal with data, which obviously takes Uris but how does this mesh with the initLoader() method? Must I set up each of my Fragments to use Loaders individually? And how unique does the id need to be for each loader, is it over the scope of my app or just a fragment? Is there any simple way of simply passing a Uri to a CursorLoader to query my data?

我可以在此刻看到的是,装载机增加不必要的额外的步骤,以获得我的数据到我的应用程序,所以有人可以解释他们对我好?

All I can see at the moment is that Loaders add an unnecessary extra step to getting my data into my app, so can someone explain them to me better?

推荐答案

有两个关键的好处使用 CursorLoader 在应用程序中超过活动.managedQuery()

There are two key benefits to using a CursorLoader in your app over Activity.managedQuery():

  1. 在该查询在后台线程你(礼貌是建立在 AsyncTaskLoader ),这样大的数据的查询不会阻止用户界面处理。这是后话了文档推荐使用纯光标,但现在它的引擎盖下完成的,当你做自己。
  2. CursorLoader 的自动更新。除了执行初始查询时, CursorLoader 注册了一个 ContentObserver 与您请求的数据集,并呼吁的forceload()本身在数据集的变化。这将导致你得到异步回调随时随地的数据变化,以便更新视图。
  1. The query is handled on a background thread for you (courtesy of being build on AsyncTaskLoader) so large data queries do not block the UI. This is something the docs recommended you do for yourself when using a plain Cursor, but now it's done under the hood.
  2. CursorLoader is auto-updating. In addition to performing the initial query, the CursorLoader registers a ContentObserver with the dataset you requested and calls forceLoad() on itself when the data set changes. This results in you getting async callbacks anytime the data changes in order to update the view.

每个装载机实例也通过奇异 LoaderManager 处理的,所以你还是没有管理直接光标,现在的连接可以持续甚至超越单一的活动 LoaderManager.initLoader() LoaderManager.restartLoader()让你重新与现有的装载机已经设置好您的查询,在某些情况下,立即得到最新的数据(如果可用)。

Each Loader instance is also handled through the singular LoaderManager, so you still don't have to manage the cursor directly, and now the connection can persist even beyond a single Activity. LoaderManager.initLoader() and LoaderManager.restartLoader() allow you to reconnect with an existing Loader already set up for your query and, in some cases, instantly get the latest data if it is available.

活动片段将可能现在实施 LoaderManager.Callback 接口。呼叫 initLoader()将导致 onCreateLoader()方法,在这里您将构造查询和新的 CursorLoader 举例来说,如果有必要的。该 onLoadFinished()方法,将每次新数据可用解雇,将包括最新的光标为你附加到视图或以其他方式遍历

Your Activity or Fragment will likely now implement the LoaderManager.Callback interface. Calling initLoader() will result in the onCreateLoader() method where you will construct the query and a new CursorLoader instance, if necessary. The onLoadFinished() method will be fired each time new data is available, and will include the latest Cursor for you to attach to the view or otherwise iterate through.

此外,还有所有这些配件一起在 LoaderManager 类文档页面的pretty的很好的例子: <一href="http://developer.android.com/reference/android/app/LoaderManager.html">http://developer.android.com/reference/android/app/LoaderManager.html

In addition, there is a pretty good example of all this fitting together on the LoaderManager class documentation page: http://developer.android.com/reference/android/app/LoaderManager.html

希望帮助!

这篇关于什么是CursorLoaders的好处是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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