我应该使用 Cursor 还是 CursorLoader?

Should I use a Cursor or a CursorLoader?

我有一个 android 应用程序,其中有一个登录系统和一些与服务器通信的其他东西。有时我只是从网络服务器得到一个确认信息,有时我会得到很多数据。到目前为止,我使用的是一个简单的数据库。今天我实现了一个 Content Provider,它目前运行良好。为了从 ContentProvider 获取数据,我使用了这个 Cursor cursor = getContentResolver().query();,但我看到还有使用 CursorLoader 的选项。它们之间有什么区别?就我而言,我应该使用什么?我还看到我必须在每个 class cursorLoader 中实现它,我不能为它制作一个 class 并在需要时调用它吗?

documentation 所述,

CursorLoader implements the Loader protocol in a standard way for querying cursors, building on AsyncTaskLoader to perform the cursor query on a background thread so that it does not block the application's UI.

这是使用Loader最大的优势,就是异步。还提到了其他一些重要优点 here.

  1. They provide asynchronous loading of data.
  2. They monitor the source of their data and deliver new results when the content changes.
  3. They automatically reconnect to the last loader's cursor when being recreated after a configuration change. Thus, they don't need to re-query their data.

如果您通过直接查询内容提供者来使用默认游标,那么您需要关闭它们,正如您所说的,您有大量数据,您必须 运行 不同的查询代码线。对于所有这些目的,使用 CursorLoaders 更加简单和高效。有关如何使用的代码,请查看 this

关于你的第二个问题,

I also saw that I have to implement it in every class the cursorLoader, can't I make a single class for it and call it when it's needed ?

你可以很好地创建一个 Base class 来实现加载器回调,然后你可以从所有需要使用CursorLoaders.