如何保持整个数据应用持久

人气:1,233 发布:2022-09-11 标签: xml android storage

问题描述

我设计的应用程序,它需要解析大型XML并保持整个应用程序访问的序列化的数据。我打算有一个数据对象,将保持所存储的数据和每个组件(尽管不是每一个)可以访问该数据。

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.

我想这个数据是非永久性的,从而应用程序解析XML,并保持在存储器中的数据。请注意,此数据会很大(XML文件> 2MB)。另外,我想数据,当用户切换到另一个应用程序在那里。

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:

静态对象单身扩展应用程序使用服务使用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?

编辑:如果数据是持久或不?使其持久意味着将有,理论上是XML的一种解析,串行化它,在数据库中存储的数据,并可以使用一个目的是从组件访问该数据。你意下如何?

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?

编辑2:我想办法,我要保持数据访问整个应用程序是使用将存储数据的SQLite数据库

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.

使用XML文件,我会分析数据,并使用SQLiteOpenHelper的子类创建把它在数据库上首次推出。当所需要的数据,我会进行查询使用使用读访问子类中的数据库。各成分(活性/服务/等等)将有自己的SQLiteOpenHelper的实例进行查询的数据库,并因此具有对数据的访问。请问这个声音?

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?

推荐答案

我想办法,我要保持数据访问整个应用程序是使用将存储数据的SQLite数据库。

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.

使用XML文件,我会分析数据,并使用SQLiteOpenHelper的子类创建把它在数据库上首次推出。当所需要的数据,我会进行查询使用使用读访问子类中的数据库。各成分(活性/服务/等等)将有自己的SQLiteOpenHelper的实例,使查询数据库。

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.

465