J2ME RMS-打开/关闭记录存储的最佳实践? [英] J2ME RMS - Best practice for opening/closing record store?

查看:153
本文介绍了J2ME RMS-打开/关闭记录存储的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Midlet使用两个唱片店.目前,我会在应用启动时创建/打开两个记录存储,并在应用的整个生命周期中都将它们保持打开状态.

My midlet uses two record stores. Currently, I create/open both record stores when the app starts and I leave them both open for the entire lifetime of the app.

如果我在每次操作(例如,读取或写入)后打开/关闭记录存储,则在模拟器中的延迟确实很糟糕.

If I open/close the record store after each operation (e.g., reading or writing) the delays are really bad in the emulator.

类似地,如果我在应用程序退出时关闭记录存储,则会有另一个非常长的延迟.

Similarly, if I close the recordstores when the app exits, there is another very long delay.

对于我来说,永远不要关闭代码中的记录存储就可以了(因此,假定设备在应用程序退出时将自行执行此操作).如果没有,那么我可以采用的最佳实践是什么,而又不会给用户带来明显的延迟,并且又不会造成任何数据丢失的风险?

So is it OK for me to never close the record stores in my code (thereby, presuming the device will do this itself when the app exits). If not, what is the best practice I can employ without causing a noticeable delay for the user and without risking any data loss?

文档与此相关,而我在Google上找不到任何内容.

There is nothing in the docs regarding this, and nothing I could find on google.

推荐答案

据我所知,在某些手机上,只有在关闭数据库后,数据库中的更改才会永久存储.在大多数J2ME实现中,更改是在每次记录更改时保存的.

As far as I remember, on some phones changes in DB are stored permanently only when DB is closed. While in most J2ME implementations changes are saved on each record change.

如果可以显着提高性能,我建议保持数据库在整个应用程序会话中保持打开状态.当然,值得在destroyApp()中处理数据库关闭.

I would suggest keeping DB open for whole app session, if it significantly improves performance. It is worth handling DB close in destroyApp() of course.

您还可以考虑实现自动保存"功能-如果IO一段时间不活动,则关闭并重新打开数据库.

You also can consider implementing 'auto save' feature - close and reopen DB if IO is inactive for some time.

通常,仅在某些操作中执行繁重的数据库访问,而不是连续执行.在这种情况下,您可以将一堆IO操作包装在一个事务"中,并在数据库关闭时完成它.

Usually heavy DB access is performed in some actions only, not constantly. In this case you could wrap bunch of IO operations in a 'transaction' finishing it with DB close.

换句话说,在大多数设备上,您可以采用第一种方法(保持数据库处于打开状态),但是在某些设备上(可能记不清,可能是在诺基亚S40或S60上),当应用程序处于关闭状态时,这可能会导致数据丢失.如果没有适当的数据库关闭,则由VM终止(由于无法保证不会销毁destroyApp,因此无法处理).因此,通常情况下,使用DB.close()语句包装关键事务是正确的

In other words, on most devices you can go with the first approach (keeping DB open) but on some devices (do not remember exactly, probably on Nokia S40 or S60) it can lead to data loss when the app will be terminated by VM (and you can't handle it since destroyApp is not guarantied to be called), without proper DB close. So in general case it would be right to wrap critical transactions with DB.close() statements

这篇关于J2ME RMS-打开/关闭记录存储的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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