Search code examples
androidandroid-sqlitesharedpreferenceskey-valuedesign-principles

How should I store ~400 key-value pairs for my Android app?


What would be the best way to store ~400 key-value pairs for an Android app? These values would be set on initial setup of the app and changed extremely infrequently after that. However they are used once per app use as a template to create a set of other objects which are then manipulated.

In the previous iteration of the app I stored these values in a separate sql table. I am now transitioning to using the room model and reworking a lot of the code, which part of that is evaluating previous decisions.

How the previous code worked was that I would import the ID and boolean value pair into a list of objects. The user when then add additional info to those objects and then I would re-store those objects data back into a separate, but related sql table.

Should I keep the values in the table? Would it be too much or inefficient to use SharedPreferences? Is there a better way that I am unaware of?

Here is the format of the two tables. The first is keeping track of which carts are currently in rotation whereas the second table is a daily historical record of each cart.

(cartID int NOT NULL PRIMARY KEY, 
in_rotation boolean NOT NULL) ");

database.execSQL("create table " + cartlistTable + " 
(checksheetID string NOT NULL, 
cartID int NOT NULL, 
in_rotation boolean NOT NULL, 
checked boolean default 0, 
overdue boolean default 0, 
maintenance boolean default 0, 
FOREIGN KEY(checksheetID) REFERENCES checksheet(checksheetID), 
FOREIGN KEY(cartID) REFERENCES carts(cartID))");

Solution

  • I would suggest you to keep away from shared preference if you are working with large amount of data, because the maximum amount of data that can be stored in shared Preference is 1428.51 kb.

    Also shared preference is stored in xml file and it lacks the strong transaction support from SQlite or Room. Shared preferences loads all the data reads that entire XML file's contents into memory.

    Comparing room and shared preferences, for information like 400 items, You can go with room.