0

I am developing a simple android app in which I need to cache 4 ArrayLists and each has 50 objects. It may contain up to 1MB data. For this, I am using SharedPreference.

Is this fine to use SharedPreference for my use case or should I use Internal Storage(FileOutputStream)?

What is the size limit of SharedPreference?

I don't want to use SQLite for such small amount of data and it will complicate the code.

Sonam Pasi
  • 562
  • 1
  • 4
  • 19
  • "Is this fine to use SharedPreference for my use case or should I use Internal Storage?" -- `SharedPreferences` and SQLite databases are both stored on internal storage by default. "Internal storage" is not a type of file. – CommonsWare Jun 13 '17 at 22:24
  • Ok, I just edited my question. – Sonam Pasi Jun 13 '17 at 22:30
  • [have a look at this](https://stackoverflow.com/questions/7057845/save-arraylist-to-sharedpreferences) i think this is what you beed – Naser Khoshfetrat Jun 13 '17 at 22:41
  • @nasser I have already gone through this. I want to know how much data I can store in SharedPreference, is there any limit? – Sonam Pasi Jun 14 '17 at 06:14
  • If you feel that your arraylists deserve better structuring and may scale up in the future, use Sqlite (or better, use ORM). If you feel you need simple getter, setter functionality, use SharedPreferences. However, make sure size constraints exist for the app in the future, since SharedPreferences may throw error. [See here](https://stackoverflow.com/questions/23311359/android-sharedpreferences-size-limit) – Kunal Chawla Jun 15 '17 at 09:35
  • Yes, My need is simple. My app is showing top 50 results for which I am using ArrayList. I am using APIs for fetching the data from the server. I just need to cache this data locally so a user can see it when the internet connection is not available. – Sonam Pasi Jun 17 '17 at 13:47

2 Answers2

0

the lowest size limit that I am aware of will be your amount of free heap space, as SharedPreferences reads that entire XML file's contents into memory. Since SharedPreferences are stored in an XML file, and therefore lacks the strong transaction support of SQLite . if you are storing more than 100k go with sqlite

-1

based on @CommonsWare answer in this post

Since SharedPreferences are stored in an XML file, and therefore lacks the strong transaction support of SQLite, I would not recommend storing "100KBS" in SharedPreferences.

That being said, the lowest size limit that I am aware of will be your amount of free heap space, as SharedPreferences reads that entire XML file's contents into memory.

i think its better if you use SQLite databases

Community
  • 1
  • 1
  • What about writing the data to a text file? [see this answer](https://stackoverflow.com/a/21460508/2970035) – Sonam Pasi Jun 14 '17 at 20:32
  • There is no limits on any resources now after Android2.3 release the new aapt – Naser Khoshfetrat Jun 14 '17 at 20:42
  • Do you mean that I can use SharedPreference now? Can you share from where did you get this information? – Sonam Pasi Jun 15 '17 at 09:28
  • "7.6.2. Application Shared Storage Device implementations MUST offer shared storage for applications. The shared storage provided MUST be at least 1GB in size." read it from https://source.android.com/compatibility/2.3/android-2.3.3-cdd.pdf so based on this you dont have predefined limit just you use whats remain of storage after previous app used – Naser Khoshfetrat Jun 16 '17 at 11:19