How to keep data persistent throughout the application

戏子无情 提交于 2019-12-08 09:54:42

问题


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?


回答1:


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

  • How do I pass data between Activities in Android application?
  • How to send an object from one Android Activity to another using Intents?
  • http://developer.android.com/guide/faq/framework.html#3



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/11930775/how-to-keep-data-persistent-throughout-the-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!