Android – Store data in local storage and update in interval of time
Background
I am a beginner in Android native app development. Recently one requirement from client made me to write this post. App that I am working has already had one web service call to load data using an AsyncTask. So as of now app calling web service each time when page opens. For improving performance of App we need to store this in local storage and storage should be updated in an interval of time. So there are lot of ways these can be achieved. Here I am explaining steps followed to achieve this in less update in existing code.
Solution
Here I am explaining how do we store json data locally after Async call and retrieve those stored data whenever needed. So local storage is updated in intervals say in each 8 hours.
For storing webservice data we need to create one shared preference variable and one more shared preference variable needed for tracking when is then last web service called.
Create two shared preference variable in Activity where we are calling web service. One for storing json data and another for storing date on which last web service called.
Throughout this article we will be using Json data term for web service response
Following are the steps that we need to do when activity loading.
1. Step 1 – load stored json data preference variable
2. Step 2 – load last web service called date and time from date preference variable
3. Step 3 – Check whether json stored is null if null call web service and store date and json data in local variable
4. Step 4 – if json data is present , load last stored date and find difference this date time to current date time
5. Steps 5 – if difference in hours is more 8 hours call web service and store latest data and last updated date and time in local preference variable.
6. Step 6 – If difference is less than 8 hours take json data from local preference variable and load to required variable for further processing.
We have to start by initializing two stored preference variable in activity here we say JsonData for storing web service response and last_st_time preference variable for storing date and time when last web service called.
So as first step we are going to add some code in which function we are calling web service. So this code will decide whether we need to call web service or not. That is if last web service called before 8 hour then we have to call web service again or else we will take JsonData from preference variable without calling web service.
Se below source code
String st; // initialsing a string variable
st = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("JsonData",null);
//===========get last stored date================//
//Date myDate = new Date(prefs.getLong("time", 0));
Date laststoredDate = new Date(PreferenceManager.getDefaultSharedPreferences(getActivity()).getLong("last_st_time",0));
//long millis = date.getTime();
// Log.v("TIME TEST", Long.toString(laststoredDate));
Log.v("TIME TEST", laststoredDate.toString());
Date currenDate = new Date(System.currentTimeMillis());
long diff = currenDate.getTime() - laststoredDate.getTime();
long seconds = diff / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
long days = hours / 24;
Log.v("TIME TEST Difference", "vale-"+days+"hours--"+hours+"minutes--"+minutes);
//===========get stored date================//
if(hours>8){
async.execute(“https://www.yourdomain.com/api/json-list?ver=1”);
}
Now we are going to focus on code part where control goes after successfully calling webservice. In this place we have to store response in local variable with date and time.
See below code snippet for this
//=======storing Jason data too local =====
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putString("JsonData",outputObject.get(0).toString()).apply();
stationList = new Gson().fromJson(PreferenceManager.getDefaultSharedPreferences(getActivity()).getString("JsonData",""), YourClass.class);
//=============store date ============//
//getting the current time in milliseconds, and creating a Date object from it:
Date date = new Date(System.currentTimeMillis()); //or simply new Date();
PreferenceManager.getDefaultSharedPreferences(getActivity()).edit().putLong(“last_st_time”,date.getTime()).apply();
//=============store date ============//
Conclusion
Android provides several options for you to save your app data. The solution you choose depends on your specific needs, such as how much space your data requires, what kind of data you need to store, and whether the data should be private to your app or accessible to other apps and the user.
What here I explained is may not be a perfect method for a new project. This method you can use if you want to store data private to app as key value pair and without changing much on current functionality of your app.