简体   繁体   English

如何在整个应用程序中保持数据持久性

[英]How to keep data persistent throughout the application

I'm designing an app which involves parsing a large XML and keeping the serialised data accessible throughout the application. 我正在设计一个应用程序,其中涉及解析大型XML并保持序列化的数据可在整个应用程序中访问。 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. 我希望这些数据是非持久性的,从而应用程序可以解析XML并将数据保留在内存中。 Note this data will be large (XML file is >2MB). 请注意,此数据将很大(XML文件> 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) 使用SQLite数据库(我不想这样做)

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. 从理论上讲,使其具有持久性将意味着对XML进行一次解析,对其进行序列化,将数据存储在数据库中并可以使用对象从组件中访问该数据。 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. 编辑2:我认为要使数据在整个应用程序中均可访问的方式是使用SQLite数据库来存储数据。

Using the XML file, I will parse the data and put it in the database on first launch using a created subclass of SQLiteOpenHelper. 使用XML文件,我将使用创建的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. 每个组件(活动/服务/等)将具有其自己的SQLiteOpenHelper实例,以对数据库进行查询,从而可以访问数据。 How does this sound? 听起来如何?

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. 顺便说一句,如果要删除值,也可以使用editor.remove(PREFS_LOGIN_USER) also call editor.commit(); 还调用editor.commit(); to persist changes. 坚持改变。

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. 我认为要使数据在整个应用程序中都可访问的方式是使用将存储数据的SQLite数据库。

Using the XML file, I will parse the data and put it in the database on first launch using a created subclass of SQLiteOpenHelper. 使用XML文件,我将使用创建的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. 每个组件(活动/服务/等)将具有其自己的SQLiteOpenHelper实例,以对数据库进行查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 android:如何加载大量数据(xml文件)并将其保留在整个应用程序中 - android: How to load lot of data (xml file) and keep it throughout the application 如何在整个Android应用生命周期内维护持久性数据? - How to maintain persistent data throughout android app lifetime? 如何在Android应用程序的整个生命周期中保持全局db4o - How to keep a global db4o throughout the lifecycle of an android application 在应用程序的整个活动中管理持久的可视数据(事件驱动的按钮和ImageView) - managing Persistent Visual Data (event driven buttons and ImageViews) throughout Activities of an application 如何使用Firebase通过我的Android应用程序保持用户持久登录 - How to keep a user persistent logged in through my Android application with Firebase 使数据在片段之间保持持久 - keep data persistent between fragments 如何在整个应用程序中显示小吃店? - How to show snackbar throughout the Application? 如何保持 CSS 在 WebView 中持续存在 - How to keep CSS persistent in WebView 如何保持临时演员的价值观持久 - how to keep the values in the extras persistent 在整个应用程序中保持XMPP连接(使用smack) - Keep XMPP connection(using smack) alive throughout application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM