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

编辑:更新的答案以反映更新的问题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 / CursorLoader或者自己关闭().不这样做会泄漏内存,因为Cursor背后有本机资源(例如数据库的文件句柄),GC无法帮助解决这个问题.

2) Cursor also has a .deactive() method and the documentation says it

uses less resources (than active cursors). When exactly should I use

this? …

编辑给其他读者:OP找到答案并在他的问题中发布.以下仍然适用:

我从未使用过deactivate()(没有deactive()),也许别人可以解释这个.如果你想要真正无痛的重新查询/更新,请查看LoaderManager框架 – 它不仅适用于Honeycomb:使用compat库你可以使用LoaderManager(和Fragments)到Android 1.6.它不仅减少了你编写的代码,而且完全将这些东西卸载到Android上,远远超过了startManagingCursor().

EDIT2:关于LoaderManager的一些注意事项

developer.android.com上有LoaderManager教程,但是这些教程非常复杂且很难理解,就像大多数教程一样.我也不得不挖掘很多,到目前为止我找到的最好的一体化停止是http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/(加上你能找到的所有javadocs和compat lib源码)— LoaderManager的工作方式非常类似于(现在也是如此)已弃用,由DialogFragment取代)托管对话框及其onCreateDialog,onPrepareDialog方法,您只需告诉Android“显示对话框#123”,然后Android使用该ID调用您的代码;同样适用于加载器:“load loader#123”,Android调用onCreateLoader().

唯一明显的缺点是,最初,LoaderManager严重依赖于ContentProvider框架,有些人似乎真的不喜欢它.当然,它是额外的学习和代码,但是一旦你拥有了一个ContentProvider用于你自己的数据(即使只在你的应用程序中私下使用),所有数据到视图的绑定都是轻而易举的CursorLoader.恕我直言,滚动你自己的“内容提供者”和实际实现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()的调用相匹配 – 一旦你摆脱了它,你通常不想承担再次管理Cursor的负担.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值