- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
为了在我的应用程序中实现数据库访问,我遵循了 Lars Vogel tutorial ,但我对一些事情感到很困惑......
每次调用 fetchTodo
时,都会创建并返回一个新游标。将之前的游标留给垃圾收集器。因此,如果我不为此使用 startManagingCursor
甚至 CursorLoader
,我是否应该在光标上调用 .close()
我完成了吗?当然在 fetchTodo
范围之外,例如:
游标 cursor = mNotesAdapter.fetchTodo();//做一点事...cursor.close();
我已经完成了这个游标,新的游标将在下一次获取时创建,我应该像这样关闭它还是应该将它留给垃圾收集器?虽然我认为我在谈论 2 件完全不同的事情......重点是,我是否应该像上面的示例那样关闭它?
Cursor
也有一个 .deactivate()
方法,文档说它使用的资源更少(比 Activity 游标)。我应该什么时候使用它?例如,在我的应用程序中,我有一个 ListActivity
,它通过 SimpleCursorAdapter
填充(为此代码初始化只调用一次)。正在使用的游标是一个类成员变量,因为我在填充列表的方法之外需要它。当从数据库中删除某些内容时,我需要它重新查询数据库。但是在删除记录之前,这是一个用户操作,可能需要一段时间才能发生,我是否应该同时停用光标?因为当我再次调用 .requery()
时它会再次激活。或者 SimpleCursorAdapter
会因为游标不活动而停止工作?编辑: 我刚刚测试了这个,发现在设置游标适配器后我无法调用 deactivate()
。如果光标未处于 Activity 状态,列表将为空,因此只要显示 ListActivity,它就需要保持 Activity 状态。最后,我们应该让 StartManagingCursor
处理它。或者新的 CursorLoader
。
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
/CursorLoader
或 close()
它自己。不这样做会泄漏内存,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
取代)托管对话框非常相似使用它们的 onCreateDialog
、onPrepareDialog
方法,您只需告诉 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/
我需要将文本放在 中在一个 Div 中,在另一个 Div 中,在另一个 Div 中。所以这是它的样子: #document Change PIN
奇怪的事情发生了。 我有一个基本的 html 代码。 html,头部, body 。(因为我收到了一些反对票,这里是完整的代码) 这是我的CSS: html { backgroun
我正在尝试将 Assets 中的一组图像加载到 UICollectionview 中存在的 ImageView 中,但每当我运行应用程序时它都会显示错误。而且也没有显示图像。 我在ViewDidLoa
我需要根据带参数的 perl 脚本的输出更改一些环境变量。在 tcsh 中,我可以使用别名命令来评估 perl 脚本的输出。 tcsh: alias setsdk 'eval `/localhome/
我使用 Windows 身份验证创建了一个新的 Blazor(服务器端)应用程序,并使用 IIS Express 运行它。它将显示一条消息“Hello Domain\User!”来自右上方的以下 Ra
这是我的方法 void login(Event event);我想知道 Kotlin 中应该如何 最佳答案 在 Kotlin 中通配符运算符是 * 。它指示编译器它是未知的,但一旦知道,就不会有其他类
看下面的代码 for story in book if story.title.length < 140 - var story
我正在尝试用 C 语言学习字符串处理。我写了一个程序,它存储了一些音乐轨道,并帮助用户检查他/她想到的歌曲是否存在于存储的轨道中。这是通过要求用户输入一串字符来完成的。然后程序使用 strstr()
我正在学习 sscanf 并遇到如下格式字符串: sscanf("%[^:]:%[^*=]%*[*=]%n",a,b,&c); 我理解 %[^:] 部分意味着扫描直到遇到 ':' 并将其分配给 a。:
def char_check(x,y): if (str(x) in y or x.find(y) > -1) or (str(y) in x or y.find(x) > -1):
我有一种情况,我想将文本文件中的现有行包含到一个新 block 中。 line 1 line 2 line in block line 3 line 4 应该变成 line 1 line 2 line
我有一个新项目,我正在尝试设置 Django 调试工具栏。首先,我尝试了快速设置,它只涉及将 'debug_toolbar' 添加到我的已安装应用程序列表中。有了这个,当我转到我的根 URL 时,调试
在 Matlab 中,如果我有一个函数 f,例如签名是 f(a,b,c),我可以创建一个只有一个变量 b 的函数,它将使用固定的 a=a1 和 c=c1 调用 f: g = @(b) f(a1, b,
我不明白为什么 ForEach 中的元素之间有多余的垂直间距在 VStack 里面在 ScrollView 里面使用 GeometryReader 时渲染自定义水平分隔线。 Scrol
我想知道,是否有关于何时使用 session 和 cookie 的指南或最佳实践? 什么应该和什么不应该存储在其中?谢谢! 最佳答案 这些文档很好地了解了 session cookie 的安全问题以及
我在 scipy/numpy 中有一个 Nx3 矩阵,我想用它制作一个 3 维条形图,其中 X 轴和 Y 轴由矩阵的第一列和第二列的值、高度确定每个条形的 是矩阵中的第三列,条形的数量由 N 确定。
假设我用两种不同的方式初始化信号量 sem_init(&randomsem,0,1) sem_init(&randomsem,0,0) 现在, sem_wait(&randomsem) 在这两种情况下
我怀疑该值如何存储在“WORD”中,因为 PStr 包含实际输出。? 既然Pstr中存储的是小写到大写的字母,那么在printf中如何将其给出为“WORD”。有人可以吗?解释一下? #include
我有一个 3x3 数组: var my_array = [[0,1,2], [3,4,5], [6,7,8]]; 并想获得它的第一个 2
我意识到您可以使用如下方式轻松检查焦点: var hasFocus = true; $(window).blur(function(){ hasFocus = false; }); $(win
我是一名优秀的程序员,十分优秀!