Android 重新使用光标

发布于 2024-12-01 13:49:12 字数 131 浏览 1 评论 0原文

在android中重用游标对象对数据库进行多个查询是一个好习惯吗?我需要在查询之间调用 deactivate() 吗?我在第二个查询后停用游标,但在 logcat 中仍然收到 DatabaseObjectNotClosedException 警告。

Is it good practice to re-use a cursor object in android to make multiple queries on a database? Do I need to call deactivate() between queries? I am deactivating the cursor after the second query but I am still getting DatabaseObjectNotClosedException warnings in logcat.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2024-12-08 13:49:12

requery()deactivate() 现已弃用(第一个自 API 11 以来,后者自 API 16 以来)。我回答这个近 7 年前的问题是因为我相信目前还没有给出正确的答案。

根据我的经验,重复使用 Cursor 对象是一种不好的做法。我的 SQLiteOpenHelper 类的实现有多个方法,并且我关闭了 <所有这些中的 code>Cursor 对象 - 我相信,由于我在每个方法末尾关闭了 Cursor 对象,所以它是正确的。但在某些方法中,我重复使用一个 Cursor 对象来保存多个查询的结果(首先我运行一个查询,然后读取并使用 Cursor 的结果) >,然后我运行了一个不同的查询,我认为它会简单地覆盖已使用的游标中的旧结果)。启用 StrictMode 证明我错了。我开始遇到 DatabaseObjectNotClosedException 崩溃。

我通过不再重复使用 Cursor 对象来修复它。现在,我为每个查询创建一个新的 Cursor 变量,然后读取并保存结果,然后在 Cursor 上调用 close() 方法并且不再使用它。如果我需要在该方法中运行另一个查询,我会故意不再使用旧的 Cursor,并且总是创建一个新变量。即使启用了 StrictMode,我也不会再收到任何警告或崩溃。我相信垃圾收集器正确地完成了它的工作,所以我不认为创建 Cursor 类的多个实例是一个大问题。另一方面,仅重用一个 Cursor 对象似乎是一个问题 - 因此您的警告和我的 StrictMode 崩溃。

我不是数据库专家,我仍在学习 Android 编程的最佳实践,但我相信在这个主题上我是正确的 - 不要重复使用 Cursor 对象,否则你可以开始出现内存泄漏、警告,甚至可能崩溃。

Both requery() and deactivate() has been deprecated now (the first one since API 11, the latter since API 16). I'm answering this almost 7 years old question because I believe there's not a correct answer given yet.

In my experience, re-using a Cursor object is a bad practice. My implementation of SQLiteOpenHelper class has several methods, and I close the Cursor object in all of those - and I believed that since I closed the Cursor object at the end of each method, it was correct. But in some methods I was re-using one Cursor object for saving results from multiple queries to it (first I ran a query, then I read and used the result from the Cursor, and then I ran a different query which I thought would simply overwrite the old result in an already used Cursor). Enabling StrictMode proved me wrong. I started getting DatabaseObjectNotClosedException crashes.

I fixed it by not re-using the Cursor object anymore. Now I create a new Cursor variable for each query, then read and save the results, and the I call close() method on the Cursor and don't use it anymore. If I need to run another query in that method, I deliberately don't use the old Cursor anymore, and I always create a new variable. I'm not getting any more warnings or crashes even when StrictMode is enabled. I believe that garbage collector does it's job correctly, so I don't think that creating multiple instances of Cursor class is a huge issue. Re-using only one Cursor object, on the other hand, like an issue seems - hence your warnings and my crashes with StrictMode.

I'm not a database expert and I'm still learning best practices in Android programming, but I believe that on this topic I'm correct - don't re-use Cursor objects, or you can start getting memory leaks, warnings and possibly even crashes.

终弃我 2024-12-08 13:49:12

根据我的经验,您只需将光标设置到新的位置即可。我尝试对其调用 deactivate() ,但这弄乱了我的应用程序,所以我决定不使用它。我不知道这是否是一个好的做法,但它对我来说效果很好,而且就我的测试而言,它似乎没有泄漏或减慢任何东西。

In my experience, you can just set the cursor to something new. I've tried calling deactivate() on it, but that messed up my app, so I decided not to use that. I don't know if it's good practice or not, but it worked fine for me and as far as my testing goes, it doesn't seem to leak or slow down anything.

无需解释 2024-12-08 13:49:12

嗯,requery() 已被弃用,但由于某种原因 deactivate() 没有被弃用。你不能真正使用一个而没有另一个,所以我想你可以假设你不应该使用 deactivate() 。无论如何,使用完后立即关闭游标将消除所有那些讨厌的DatabaseObjectNotClosedException。新的加载器框架鼓励返回新的光标,当您替换旧的光标时,旧的光标会自动关闭。

Well, requery() has been deprecated, bur for some reason deactivate() is not. You can't really use one without the other, so I guess you could assume you shouldn't use deactivate() . In any case, closing a cursor as soon as you are done with it will get rid of all those pesky DatabaseObjectNotClosedException's. The new loader framework encourages returning new cursors, old ones are automatically closed when you replace them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文