多久运行getWritableDatabase()和getReadableDatabase()? [英] How often to run getWritableDatabase() and getReadableDatabase()?

查看:142
本文介绍了多久运行getWritableDatabase()和getReadableDatabase()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个服务,内容提供商,和多个应用程序。服务新的数据写入到内容提供商的SQLite数据库每5分钟左右,再加上用户的输入,并拟在后台多一直运行pretty。该应用程序,运行时将显示来自内容提供商拉数据,每当服务将更多的数据到内容提供商的数据库将被刷新。

I'm writing a Service, a Content Provider, and multiple apps. The Service writes new data to the Content Provider's SQLite database every 5 minutes or so plus at user input, and is intended to run pretty much forever in the background. The app, when running, will display data pulled from the Content Provider, and will be refreshed whenever the Service puts more data into the Content Provider's database.

每五分钟一次,当是调用SQLiteOpenHelper的 getWritableDatabase() / getReadableDatabase(正确的时间)?它是在的onCreate()内容提供商,或者我应该每次有一次运行该程序的插入()插入()?被插入的数据每5分钟将包含多个插件。

Given that the Service only inserts into the database once every five minutes, when is the right time to call SQLiteOpenHelper's getWritableDatabase() / getReadableDatabase()? Is it on the onCreate() of the Content Provider, or should I run it every time there is an insert() and close it at the end of insert()? The data being inserted every 5 minutes will contain multiple inserts.

推荐答案

对于您的具体问题,如果你打算使用的ContentProvider 这种方式,你可以希望去开闭,每插入模式。有一个在的ContentProvider 没有的onDestroy()挂钩,所以没有伟大的时间来关闭数据库。更频繁/随机访问模式,你就必须忍受它。在你的情况,你可能也关闭它。

With respect to your specific question, if you are going to use a ContentProvider this way, you may wish to go the open-and-close-every-insert pattern. There is no onDestroy() hook in ContentProvider, so there's no great time to close the database. With more frequent/random access patterns, you just have to live with it. In your case, you may as well close it up.

话虽这么说,我不清楚为什么你用的ContentProvider 摆在首位困扰。

That being said, I'm not clear why you're bothering with a ContentProvider in the first place.

这篇关于多久运行getWritableDatabase()和getReadableDatabase()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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