0

I'm designing an app which involves parsing a large XML and keeping the serialised data accessible throughout the application. I intend to have a data object which will keep the data stored and each component (though not every one) can access the data.

I would like this data to be non-persistent, whereby the application parses the XML and keeps the data in memory. Note this data will be large (XML file is >2MB). Also, I would like the data to be there when a user switches to another app.

I have looked into possible solutions, such as:

  • Static objects
  • Singletons
  • Extending Application
  • Using a Service
  • Using a SQLite database (I don't want to do this)

I do not want to get in to the endless argument of Singletons vs. extending Application, etc. but I would like to do unit tests as well and I've heard that Singletons and static objects are hard to test.

Can anyone shed some light on this? What would be the most elegant way to do it?

Edit: Should the data be persistent or not? Making it persistent would mean that there would, in theory, be one parse of the XML, serialises it, stores the data in a database and could use an object to access that data from the component. How does that sound?

Edit 2: I think the way I am going to keep the data accessible throughout the application is to use an SQLite database which will store the data.

Using the XML file, I will parse the data and put it in the database on first launch using a created subclass of SQLiteOpenHelper. When the data is needed I will make queries to the database using the subclass using read access. Each component (Activity/Service/etc.) would have its own instance of the SQLiteOpenHelper to make queries to the database and thus have access to the data. How does this sound?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Tim Kist
  • 1,164
  • 1
  • 14
  • 38

3 Answers3

1

It is difficult to give an answer without more details. But I recommend these links:

Community
  • 1
  • 1
Rémi
  • 3,705
  • 1
  • 28
  • 39
  • Thank you, I thought about passing data through activities but that could get messy. I also had a look at the Android Framework FAQ but I found that Singletons and static data is not great. I thought it would make sense to have a place for the data to be central. – Tim Kist Aug 13 '12 at 09:11
0

Taking all of your concerns into consideration, I would use Shared Preferences to achieve what you want. Here's an example code from my app:

Modified for simplicity:

private static final String PREF_NAME = "MyPrefs"; //Any value would do
private static final String PREFS_LOGIN_USER= "user"; //Any value would do
private static final String PREFS_LOGIN_PASSWORD= "password"; //Any value would do

public void onCreate(Bundle bundle){
    super.onCreate(savedInstanceState);
    ....

    //Create a preference file
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);

    //Put values
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(PREFS_LOGIN_USER, "admin");
    editor.putString(PREFS_LOGIN_PASSWORD, "P@$$w0rd");
    editor.commit();

    //Get values
    String userName = settings.getString(PREFS_LOGIN_USER, null);
    String password = settings.getString(PREFS_LOGIN_PASSWORD, null);
}

These values will persist even if the app has been closed.

by the way, you can also use editor.remove(PREFS_LOGIN_USER) if you want to remove values. also call editor.commit(); to persist changes.

PinoyCoder
  • 1,112
  • 8
  • 13
  • Problem is that the XML file is large so would have to create many Shared Preferences for each XML. Also the data is a list of many data objects. – Tim Kist Aug 13 '12 at 09:16
  • It's not a good practice to keep a large data in-memory. you would want to persist the data inside the /data/user/com.android.yourpackage / directory if you do not want your users to be tinkering with it. this way, your application uses minimal memory and data can be accessed anytime. With your serialization concern, i have used [Xstream for android](http://jars.de/java/android-xml-serialization-with-xstream) to serialize xml to objects and vise versa. hope this helps! – PinoyCoder Aug 13 '12 at 09:36
0

I think the way I am going to keep the data accessible throughout the application is to use an SQLite database which will store the data.

Using the XML file, I will parse the data and put it in the database on first launch using a created subclass of SQLiteOpenHelper. When the data is needed I will make queries to the database using the subclass using read access. Each component (Activity/Service/etc.) would have its own instance of the SQLiteOpenHelper to make queries to the database.

Tim Kist
  • 1,164
  • 1
  • 14
  • 38