Android的数据存储 - 文件VS的SQLite数据存储、文件、Android、VS

2023-09-06 02:24:52 作者:你是我今生最爱的人

我开发了定期发送信息到外部服务器上的应用程序。我使数据的本地副本被发送,以进行备份。

I am developing an application that periodically sends information to an external server. I make a local copy of the data being sent, for backup purposes.

什么是存储在延长电池寿命方面的数据的最好的选择?每个数据提交是一个序列化对象(类有5个领域,包括日期,数字和字符串)约5K-10K。

What is the best option to store the data in terms of saving battery life? Each data submission is a serialized object (the class has 5 fields, including a date, numbers and strings) of about 5K-10K.

任何其他的想法?

推荐答案

我不知道在电池寿命方面直接只有一个标准,会是更容易管理?更少的操作来管理数据就意味着更少的CPU周期,从而延长电池寿命。

I have no idea in terms of battery life directly but one criteria would be which is easier to manage? Fewer operations to manage the data would mean fewer CPU cycles and in turn longer battery life.

我会说SQLite的选项更容易。你可以把一个日期列在SQLite表存储你的数据,这使得拆除旧呈文,你不需要任何更多的非常容易 - 并且通过本地SQL库中的所有处理。管理文件的整个负载 - 或者更糟的单个文件 - 与自己的Java code会做更多的工作。

I would say the SQLite option is easier. You can put a date column in the SQLite table which stores your data which makes removing old submissions which you don't need any more very easy - and all handled via the native SQL library. Managing a whole load of file - or worse a single file - with your own Java code would be much more work.

此外,您可以写入数据到数据库,并忘掉它,直到你需要再次阅读。如果你将数据存储在文件中,你需要制定出时,你应该是对的 Android的应用程序生命周期。如果你担心的电池,你可能不希望往往比你应该写的文件,并在内存中缓存数据,但你需要确保你没有丢失任何数据,当您的应用程序暂停或销毁的。在我看来,它更容易使用的SQLite数据库而不用担心任何这一点。

Additionally, you can write data the to database and just forget about it until you need to read it again. If you're storing data in files, you'll need to work out when you should be reading and writing files in terms on the Android application life cycle. If you're worried about battery you probably wouldn't want to write files more often than you should, and cache data in memory, but you'd need to make sure you didn't lose any data when your app is Paused or Destroyed. In my opinion it's much easier to use an SQLite database and not worry about any of this.