android - 关于Android中SQLite数据库游标的几个问题

标签 android sqlite android-cursor android-loadermanager

为了在我的应用程序中实现数据库访问,我遵循了 Lars Vogel tutorial ,但我对一些事情感到很困惑......

  1. 每次调用 fetchTodo 时,都会创建并返回一个新游标。将之前的游标留给垃圾收集器。因此,如果我不为此使用 startManagingCursor 甚至 CursorLoader,我是否应该在光标上调用 .close()我完成了吗?当然在 fetchTodo 范围之外,例如:

    游标 cursor = mNotesAdapter.fetchTodo(); //做一点事... cursor.close();

我已经完成了这个游标,新的游标将在下一次获取时创建,我应该像这样关闭它还是应该将它留给垃圾收集器?虽然我认为我在谈论 2 件完全不同的事情......重点是,我是否应该像上面的示例那样关闭它?

  1. Cursor 也有一个 .deactivate() 方法,文档说它使用的资源更少(比 Activity 游标)。我应该什么时候使用它?例如,在我的应用程序中,我有一个 ListActivity,它通过 SimpleCursorAdapter 填充(为此代码初始化只调用一次)。正在使用的游标是一个类成员变量,因为我在填充列表的方法之外需要它。当从数据库中删除某些内容时,我需要它重新查询数据库。但是在删除记录之前,这是一个用户操作,可能需要一段时间才能发生,我是否应该同时停用光标?因为当我再次调用 .requery() 时它会再次激活。或者 SimpleCursorAdapter 会因为游标不活动而停止工作?

编辑: 我刚刚测试了这个,发现在设置游标适配器后我无法调用 deactivate()。如果光标未处于 Activity 状态,列表将为空,因此只要显示 ListActivity,它就需要保持 Activity 状态。最后,我们应该让 StartManagingCursor 处理它。或者新的 CursorLoader

  1. 我知道 startManagingCursor/stopManagingCursor 已弃用,但我不针对 Honeycomb(至少暂时如此)并且我不想处理现在是新的 CursorLoader。但是在上面的教程中,到处都使用了 startManagingCursor,但是从未调用过一次 stopManagingCursor。为什么不? Android 是否以自己的方式处理该问题?在任何情况下我都应该调用 stopManagingCursor

最佳答案

编辑:更新答案以反射(reflect)更新的问题 1:

1) Every time a call is made to fetchTodo a new cursor will be created and returned. Leaving the previous cursor for the garbage collector. So, if I don't use startManagingCursor or even the CursorLoader for that matter, should I call a .close() on the cursor when I'm done with it ?

是的,您绝对应该告诉 Android startManagingCursor(),使用 LoaderManager/CursorLoaderclose()它自己。不这样做会泄漏内存,GC 对此无济于事,因为 Cursor 后面有本地资源(例如,数据库的文件句柄)。

2) Cursor also has a .deactive() method and the documentation says it uses less resources (than active cursors). When exactly should I use this? ...

EDIT 致其他读者:OP 找到了答案并将其发布在他的问题中。以下内容仍然成立:

我从未使用过deactivate() (没有 deactive()),也许其他人可以解释一下。如果您想要真正轻松的重新查询/更新,请检查 LoaderManager 框架——它不仅适用于 Honeycomb:使用兼容库,您可以使用 LoaderManager(和 Fragments) 到 Android 1.6。它不仅减少了您编写的代码,而且将这些东西完全卸载到 Android,比 startManagingCursor() 更重要。

EDIT2:关于 LoaderManager 的一些注释

developer.android.com 上有 LoaderManager 教程,但这些教程非常...复杂,而且像那里的大多数教程一样,第一次很难理解。我还必须挖掘很多东西,到目前为止我发现的最好的一站式服务是 http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/ (加上您可以找到的所有 javadoc 和兼容库源)--- LoaderManager 的工作方式与(现在也已弃用,由 DialogFragment 取代)托管对话框非常相似使用它们的 onCreateDialogonPrepareDialog 方法,您只需告诉 Android“显示对话框 #123”,然后 Android 就会使用该 ID 调用您的代码;加载程序相同:“加载加载程序 #123”,Android 调用 onCreateLoader()

最初唯一明显的缺点是,LoaderManager 严重依赖于 ContentProvider 框架,有些人似乎真的不喜欢它。当然,这是额外的学习和代码,但是一旦您拥有用于您自己的数据的 ContentProvider(即使仅在您的应用程序中私下使用),所有数据到 View 的绑定(bind)对于 光标加载器。恕我直言,推出您自己的“内容提供程序”与实际实现 ContentProvider 之间几乎没有什么区别——但这只是我极具争议的观点:)

3) I know that startManagingCursor/stopManagingCursor are deprecated but I'm not targeting Honeycomb (at least for the time being) and I don't want to deal with the new CursorLoader for now. But in the tutorial above, startManagingCursor is used everywhere, but stopManagingCursor is never called once. Why not? Does Android deals with that in it's own way? Any situation I should call stopManagingCursor?

一旦调用 startManagingCursor()Cursor 就不再是您的问题了。当您的 Activity 被销毁(用户导航离开、方向改变......)时,Android 将负责关闭 Cursor。无需将对 startManagingCursor() 的调用与对 stopManagingCursor() 的调用相匹配——您通常不想承担管理 的负担>光标一旦你摆脱了它。

关于android - 关于Android中SQLite数据库游标的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7603647/

相关文章:

java - 如何通过Android应用程序过滤蓝牙标签

android - 解析服务器 : Can not find sender for push type android

android - 如何在android中显示保存在sdcard文件夹中的图像

android - 试图重新打开一个已经关闭的对象 : java. lang.IllegalStateException :?

android - 可以附加游标吗?

Android,OpenCv,拍摄的照片尺寸与预览不匹配

java - 从 Canvas 中绘制的弧中获取坐标

c - sqlite3库 undefined reference 错误

python - 将 JSON 字符串转换为 Python 字典

node.js - Nodejs + Node-Sqlite3 安装问题