Android 重新使用光标
在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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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()
anddeactivate()
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 ofSQLiteOpenHelper
class has several methods, and I close theCursor
object in all of those - and I believed that since I closed theCursor
object at the end of each method, it was correct. But in some methods I was re-using oneCursor
object for saving results from multiple queries to it (first I ran a query, then I read and used the result from theCursor
, and then I ran a different query which I thought would simply overwrite the old result in an already usedCursor
). EnablingStrictMode
proved me wrong. I started gettingDatabaseObjectNotClosedException
crashes.I fixed it by not re-using the
Cursor
object anymore. Now I create a newCursor
variable for each query, then read and save the results, and the I callclose()
method on theCursor
and don't use it anymore. If I need to run another query in that method, I deliberately don't use the oldCursor
anymore, and I always create a new variable. I'm not getting any more warnings or crashes even whenStrictMode
is enabled. I believe that garbage collector does it's job correctly, so I don't think that creating multiple instances ofCursor
class is a huge issue. Re-using only oneCursor
object, on the other hand, like an issue seems - hence your warnings and my crashes withStrictMode
.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.根据我的经验,您只需将光标设置到新的位置即可。我尝试对其调用 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.
嗯,
requery()
已被弃用,但由于某种原因deactivate()
没有被弃用。你不能真正使用一个而没有另一个,所以我想你可以假设你不应该使用deactivate()
。无论如何,使用完后立即关闭游标将消除所有那些讨厌的DatabaseObjectNotClosedException。新的加载器框架鼓励返回新的光标,当您替换旧的光标时,旧的光标会自动关闭。Well,
requery()
has been deprecated, bur for some reasondeactivate()
is not. You can't really use one without the other, so I guess you could assume you shouldn't usedeactivate()
. In any case, closing a cursor as soon as you are done with it will get rid of all those peskyDatabaseObjectNotClosedException
's. The new loader framework encourages returning new cursors, old ones are automatically closed when you replace them.