-3

For saving the actual alarm, I figured that a small private sql database would be sufficent, I have used it before for saving data aswell. However, I can't help but think that creating a database takes a lot of power So I could use the shared preference file instead, and just use a couple of linked list or something to store a few parameters for each alarm. This would use alot less space.

But this got me thinking, what is actually the preferred way? By that I mean performance, intregrity, security and the amount of work that needs implementing either shared preference file or SQL database.

Essej
  • 892
  • 6
  • 18
  • 33
  • 1
    Please read: https://developer.android.com/guide/topics/data/data-storage.html –  Jun 15 '16 at 07:18

2 Answers2

2

Here is what I think are the pros and cons:

Shared Prefs - Useful for storing key - value pairs. The data is persisted in a private file. No concept of structured data, no standardised querying. Writing / reading performs a disk I/O operation, so not super fast.

SQLite - Useful for storing structured data. The db is stored as a private file. SQL used for querying. Performance is similar to the Shared Prefs.

I wouldn't use Shared Prefs for any kind of more complex data. If there will be searching performed on the dataset, SQLite is a no brainer. The size of the database depends solely on the amount of data being used. If you are smart about what you store and how you store it, you shouldn't see you database growing out of control.

Danail Alexiev
  • 7,624
  • 3
  • 20
  • 28
1

AFAIK SharedPreferences are always handy to faster access of data and optimized storage.Even code wise its easier to implement.

I'll suggest an idea for your scenario. For alarm you hardly require 4 properties.

  • Time
  • Description
  • RepeatMode
  • Snooze

    compile 'com.google.code.gson:gson:2.3.1'

Alarm.java

class Alarm{
  private String desc;
  private Date time;
  private boolean snooze;
  private int repeat; // 1 - daily ,2- monthly ,3 - yearly ,4 - none
}

Retrieving Alarms

import java.lang.reflect.Type; 
Gson gson = new Gson(); 
Type type = new TypeToken<List<Alarm>>(){}.getType();
List<Alarm> alarms = gson.fromJson(preferences.getString("alarm",""), type);

Adding Alarms

Gson gson = new Gson();
List<Alarm> alarms = new ArrayList<>();
alarms.add(new Alarm()); // adding alarm
String jsonAlarm = gson.toJson(alarms); //coverting list to json and saving back
editor.put("alarm",jsonAlarm);
editor.commit();
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37