什么时候应该关闭游标和DB? [英] When should I close a cursor and db?

查看:367
本文介绍了什么时候应该关闭游标和DB?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用同样的光标多次在其不同的方式自定义视图。我应该关闭游标每次使用后还是可以保持打开状态,直到视图被破坏?而同样的数据库,可以在一次持有这种观点的活动是创建打开,当活动被销毁关闭?我不断收到错误'close()方法从未明确要求对数据库......当我做如上文所述。

解决方案
  

我应该关闭游标每次使用后,我也可以把它开到   该视图被破坏?

依赖。如果游标结果是不会同时改变(例如远程连接修改DB),那么它的确定。不要忘了使用 CursorLoader 有Android的自动管理你的光标(例如,如果你的应用程序崩溃关闭游标)。

  

和相同的数据库,可以将其打开一次的活动是   持有这种观点的创建和关闭时的活性被破坏?

是的,你可以打开 onResume()和关闭的onPause()的回调,或者你知道什么时候你的数据库是不会再被查询。

  

我不断收到错误'close()方法从未明确要求   数据库......当我做如上文所述。

这是正常的只有在您的应用程序崩溃。不应该发生,如果你这样做就像我上面所说的。

I use the same cursor multiple times in the custom view in its different methods. Should I close the cursor after each use or can I keep it open until the view is destroyed? And the same for the database, can it be opened once the activity that holds this view is created and closed when the activity is destroyed? I continuously receive an error 'close() was never explicitly called on database...' when I do as described above.

解决方案

Should I close the cursor after each use or can I keep it open until the view is destroyed?

Depends. If the cursor result is not going to change meanwhile (e.g. remote connection modifying the DB), then it's OK. Don't forget to use CursorLoader to have Android manage your cursor automatically (e.g. close the cursor if your app crashes).

And the same for the database, can it be opened once the activity that holds this view is created and closed when the activity is destroyed?

Yes, you can open in onResume() and close in onPause() callbacks, or when you know your database is not going to be queried anymore.

I continuously receive an error 'close() was never explicitly called on database...' when I do as described above.

This is normal only if your app crashes. Should not happen if you're doing it like I said above.

这篇关于什么时候应该关闭游标和DB?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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