我应该什么时候调用close()上使用的ContentProvider SQLiteOpenHelper [英] When should I call close() on SQLiteOpenHelper used by ContentProvider

查看:382
本文介绍了我应该什么时候调用close()上使用的ContentProvider SQLiteOpenHelper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我用SQLiteOpenHelper来实现的ContentProvider。 查询,添加,删除等操作都是通过ContentProvider的。

In my android app, I use SQLiteOpenHelper to implements ContentProvider. Query, add, delete operations are all through ContentProvider.

但在我的Andr​​oid手机(HTC G13)之一,我发现*在目录/数据/数据​​/ [包名] .db的,沃尔玛的文件/数据库。和文件大小increate非常快与ContentProvider的操作时。它占用的用户RAM空间太多了。

But in one of my android phone(htc g13), I found *.db-wal file in directory /data/data/[package name]/databases. And the file size increate very fast when operating with ContentProvider. It occupied user RAM space too much.

建议关闭SQLiteOpenHelper解决我的问题(这是很有用的)后<一href="http://stackoverflow.com/questions/6115279/android-database-size-wont-decrease-on-htc-thunderbolt">enter链接描述这里。

It is recommended to close the SQLiteOpenHelper to solve my problem (it is useful) in post enter link description here.

不过,我想找到一个地方添加关闭()方法,因为我并没有直接使用SQLiteOpenHelper(通过ContentProvider的使用)。在ContentProvider的query()方法必须返回一个指针,而SQLiteDatabse应保持打开状态。

But I want to find a "place" to add the "close()" method since I am not using SQLiteOpenHelper directly (using through ContentProvider). query() method in ContentProvider must return a Cursor, and SQLiteDatabse should stay in open state.

我很困惑,我现在该怎么办展,以保持* .db的,威名远扬,并使用ContentProvider的正常?

I'm confused, what show I do now to keep *.db-wal gone and use ContentProvider normally?

推荐答案

您有一对夫妇的情况下,以支付:

You have a couple of cases to cover:

1)当你的应用程序完成(如输入的onDestroy()),请确保关闭所有游标,SQLiteDatabase和SQLiteOpenHelpers数据库实例(利用该模型,如果(connection.isOpen())object.close())

1) When your application finishes (e.g. entering onDestroy()) make sure you close all Cursors, Database instances of SQLiteDatabase and SQLiteOpenHelpers (using the model if (connection.isOpen()) object.close())

2)当你的应用程序去的onPause() - > onResume() - 使用此阶段适当暂停/恢复您的连接或关闭/打开它们。

2) When you application goes onPause() -> onResume() - use this stages appropriately to pause/resume your connection or to close/open them.

这是一个很好的做法,关闭数据库,在完成它的工作之后。该数据库缓存,所以没有问题,将其关闭并重新获得实例时你getWritableDatabase()/ getReadableDatabase()

It's a good practice to close your database immediately after you finish working with it. The database is cached, so there's no problem closing it and re-acquire instance again when you need it with getWritableDatabase()/getReadableDatabase()

从官方文档: 一旦成功打开,数据库高速缓存,这样你就可以每次你需要编写到数据库时调用此方法。(一定要调用close()时,你不再需要的数据库。)

另外请注意,如果SQLiteOpenHelper缓存和跟踪SQLiteDatabase的所有打开的情况下,基本上意味着,如果你不离开打开的数据库连接,您就不必调用Close SQLiteOpenHelper。

Also keep in mind that if SQLiteOpenHelper caches and tracks all open instances of SQLiteDatabase, it basically means that if you don't leave open database connections, you won't have to call close on SQLiteOpenHelper.

我建议关闭所有光标和后立即停止与他们一起工作的数据库。应尽量将强制执行的try / catch /进行查询操作和finally块来调用对象的Close方法。

I recommend closing all cursors and databases immediately after you stop working with them. Always try to enforce try/catch/ for queries operations and finally block to call the close methods on the objects.

这篇关于我应该什么时候调用close()上使用的ContentProvider SQLiteOpenHelper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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