Reputation: 822
I have an app runs in a background service all the time (only when screen is on), and on a regular intervals (1-10 minutes) records a how long the interval time was and whether it was a good or bad interval. At the end of each day I take these counts of good and bad intervals and record it to a csv file.
For visual people like me:
<--------------------------THE DAY --------------------->
minutes: 2m 1m 5m 3m 3m 3m 3m 1m 2m 1m 1m
|----|---|-------|-----|-----|-----|-----|--|----|---|---|
rec rec rec rec rec rec rec rec rec rec rec
Record
Currently I use SharedPreferences to store the good/bad count throughout the day, and write it to a file at the end of the day, but am becoming increasingly worried about the persistence of the SharedPrefs data (I've had a few cases of it being wiped randomly by what I think was the OS clearing my apps cache)
I'm attempting to decide between recording these temporary counts in a File vs. migrating completely over to an SQLite database structure and making consistent calls to write and read throughout the day.
From a battery performance perspective, which is the least costly for making multiple writing and reading calls a day (~500-5000 actions) and Why?
Upvotes: 1
Views: 898
Reputation:
Looks like file based approach will be better than sqlite, but it depends how you are handling files
SQLite itself store data on a db file. It will have its own db management cost but very tiny
every time app insert or fetch data, it will have to open db, this will be a cost itself. If app keeping it open, that is also costly
There will be less android API layer to update file directly than sq lite
Apart from all these facts, testing such a scenario should not be a problem. I will be writing a test case like this
Have a constant battery level i.e. 100% full charged
keep running some battery intensive activity i.e. playing a video , download, running a program which run continuously writing to a file (current date time secs )
Reduce regular interval to 10 secs ( from 1-10 minutes )
Update data in file every 5 minutes
Check the battery level after 2 hours
repeat test but in point 3 update db instead of files
There could be double work, as you will have to implement with both approach, but I believe updating in db will not have much coding efforts
few may not prefer 10 sec interval, as no significant change will happen in this duration, but here goal is to have as many iteration as possible , update them in file or DB and get the stats for battery level comparison.
Upvotes: 0
Reputation:
SQLite database == file based data storage
A SQLite database is just a mere file stored on disk, the performance difference will be minimal, the only thing a SQLite database does more is executing a little more instructions on the CPU to eventually get the data written.
The only reason why I wouldn't choose for a SQLite database is because maybe it's a little bit of overkill on the setup side since you're only writing numbers. What you could do instead is write a file yourself to the storage/sdcard and keep appending numbers to the file separated by newlines or some other CSV format.
Upvotes: 3