Reputation: 11098
When the application loads I do a request to a webservice that returns JSON which is converted to Plain Old Java Objects:
Airports:
public class Airports {
private List<Airport> airportList = new ArrayList<Airport>();
...get and set...
}
Airport:
public class Airport {
private String id;
private String airport;
private String city;
...get and set...
}
Now because doing a request everytime is costly, I would like this list of Airports (the airports object) to be accessible throughout the application. How can I access the data of a single instance of this object.
Storing in SQLite would be overkill.
Now I have read about using the Application Subclass, Singleton classes and Shared Preferences.
I am not too sure which solution best fits the problem?
Upvotes: 0
Views: 143
Reputation: 29285
Choosing one of the ways you have said is based on what behavior you would want about those data. If you would want stability of those data during application life cycle, using singleton or application subclass would be okay, but otherwise if you want those data to be persistent for ever until the app is installed, you probably need SQLite or file or Shared Preferences. So
Persistent during app life-cycle
Application
classPersistent until app is installed
Note that SQLite and shared preferences approach is more structured than using raw files.
Upvotes: 1
Reputation: 14027
Store the json in shared preferences if it's not too big (few hundreds sizes in kb).
SharedPreferences.Editor editor = preferences.edit();
editor.putString("jsondata", jsonData.toString());
editor.commit();
Retrieval:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String stringJson = sharedPref.getString("jsondata");
if(stringJson != null){
JSONObject jsonData = new JSONObject(stringJson);
}else{
//retrieve from web services
}
Upvotes: 1