0

SHORT QUESTION: Is it acceptable to store JSONString, returned from API Call, in Sharepreferences and use it for later use (and faster activity loading) or should I implement some kind of cache methods?

P.S Shareprefeferences will be updated with new JSONString if data is changed on Server.

LONG QUESTION: I've developed an app on Android where I'm using some online sources for data. Using Volley, I'm making API Calls and getting the response in JSONString.

So the app workflow is like this, The user opens the app and an API call is fired to fetch new data from the server, it takes between 2 - 5 seconds for API to get the response from. Now sometime the delay goes double of 2-5 seconds and the user can't do anything before the data is fetched and is available. In short, the user is stuck for 5 seconds on a blank screen with loading spinner(BAD USER EXPERIENCE). To avoid this delay, I'm saving the response from the server to Sharepreferences and load the data locally while the API call is executed in the background. Now when the API Call returns fresh data from the server, Only if the data is changed, I'm updating the Shareprefs and a toast/pop-up is displayed with some string like "refresh now".

My question is Is it ok to use shareprefes for storing this kind of data or should I use some sort of caching methods to store and view the data.

Data is mostly Strings, no images or icons so I can load from sharedprefs even if no internet is available.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Joe
  • 173
  • 1
  • 15
  • why don't you parse and store your json data to local database and then use – Sandeep dhiman Nov 25 '19 at 10:48
  • if security is not your concern then you can store strings in sharedprefs, but the preffered way is to implement using database – Neha Rathore Nov 25 '19 at 10:51
  • @NehaRathore these are simple strings, not some sensitive data so security is not a concern here. but using database will be an overkill for this simple case, this is what I think. But mostly for this part, I'm asking if there are any limitations in using shared prefs? – Joe Nov 25 '19 at 11:39
  • there is limit on size of data i guess otherwise no issues – Neha Rathore Nov 25 '19 at 12:48

2 Answers2

2

My recommendation is to use the standard caching technology available in inbuild libraries like Retrofit or Volley.

I hope you will be making use of anyone of them for your API calls, Here is one small example of doing it in Retrofit

int cacheSize = 10 * 1024 * 1024; // 10 MB  
Cache cache = new Cache(getCacheDir(), cacheSize);

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
        .cache(cache)
        .build();

Retrofit.Builder builder = new Retrofit.Builder()  
        .baseUrl("http://10.0.2.2:3000/")
        .client(okHttpClient)
        .addConverterFactory(GsonConverterFactory.create());

Retrofit retrofit = builder.build();  

For caching in Volley you can refer to Android set up Volley to use from cache

Vinay Jayaram
  • 1,030
  • 9
  • 29
  • I was using volley caching methods but it was making a mess at the time but maybe I'll switch back to this technique soon. – Joe Nov 25 '19 at 11:41
  • Good, My recommendation is to have timestamp/version stored in shared preference/sql during the time of invocation of API for the first time. You can compare this with the subsequent request to make sure your data is updated when there is change in timestamp/version. This should be quite easy to integrate. I have integrated this in many of my apps and it works great – Vinay Jayaram Nov 25 '19 at 11:45
0

Storing JSON response in Shared Preference is a bad practice. You can use a caching mechanism to store it.

Adding to it, SharedPreference has a limit of 8192 characters for each value. Hence it is not prefered to store json string

Viswanath Kumar Sandu
  • 2,230
  • 2
  • 17
  • 34
  • I doubt that this is the limit here because the string that I'm saving here in prefs is way larger than this limit. and according to [this](https://stackoverflow.com/questions/8516812/shared-preferences-max-length-of-a-single-value/43080588) SO the limit will the javastring limit. – Joe Nov 25 '19 at 11:39