简体   繁体   English

android:如何加载大量数据(xml文件)并将其保留在整个应用程序中

[英]android: How to load lot of data (xml file) and keep it throughout the application

I'm designing an app which involves parsing a large XML(which include lots of text) and keeping the serialised data accessible throughout the application. 我设计的应用程序,它需要解析大型XML(包括大量文字),并保持整个应用程序访问的序列化的数据。 I intend to have a data object which will keep the data stored and some classes can access the data. 我打算有一个数据对象,该对象将保留数据,某些类可以访问该数据。 I alway need to show only part of the text. 我总是只需要显示部分文字。

yes my problem is very similar to this question maybe it's the same problem, but I don't want to use this solution, if is it possible. 是我的问题很相似,这个问题也许是同样的问题,但我不希望使用此解决方案,如果是有可能。

I would like to know how to load the data, where to load it and how to keep it accessible throughout the application. 我想知道如何加载数据,其中加载它,以及如何保持它在整个应用程序进行访问。 It's impossible to load it everytime in onCreate() method, for example when the screen orientation is changed. 这是不可能的,每次加载它在onCreate()当屏幕方向改变的方法,例如。

Best way for me, would be if I could load (parse) the xml file into one object and this object keeps accessible throughout the application, but I don't know where, how to load it and how to keep it accessible throughout the application. 对我而言,最好的方法是,如果可以将xml文件加载(解析)到一个对象中,并且该对象在整个应用程序中始终可访问,但是我不知道在哪里,如何加载它以及如何使其在整个应用程序中可访问。 。 This object has everything what I need. 这个对象拥有一切我需要什么。

some possible solutions: 一些可能的解决方案:

  • Singleton (this would load data into one object) use this or something else ? 辛格尔顿(这将数据加载到一个对象)使用这个或其他什么东西?
  • Using a SQLite solution like there (same link as above) (I don't want use this) 使用像一个SQLite的解决方案 (同上链接)(我不想用这个)
  • Use lazy loading (load the part of text, which I actualy need) (I don't want use this as well) 使用懒加载(加载文本的一部分,这是我actualy需要)(我不想用这个为好)

Could someone say what would be best solution ? 可能有人说,这将是最好的解决办法? And describe how to do it ? 并描述该怎么做?

(Sorry for my english) (对不起我的英语不好)

  • EDIT 1: xml structure: 编辑1: xml结构:

     < root > < some elements > < /some elements > < element with lot of text > text..... < /element with lot of text > < element with lot of text > text..... < /element with lot of text > . . . < element with lot of text > text..... < /element with lot of text > < /root > 

if it will be helpful. 如果有帮助。 Text in one element usually includes 5000 - 15000 characters. 一个元素中的文本通常包含5000-15000个字符。

Singletons have various general disadvantages; 单身人士有各种普遍的缺点; I'd personally advise against using them for anything that is not stateless and independent of everything else (including files or the network, where you load your XML from). 我个人建议不要将它们用于并非无状态且独立于其他所有事物的任何事物(包括从中加载XML的文件或网络)。

The main problem in regards to your application is, however, that Android is allowed to start and stop your application at will. 关于您的应用程序的主要问题是,但是,允许Android随意启动和停止您的应用程序。 That means, that your singleton would need to get re-created often, eg you'd need to parse your data quite often (restarts do not preserve singletons!). 这意味着,您的单例将需要经常重新创建,例如,您需要非常频繁地解析数据(重新启动不会保留单例!)。

Without knowing anything about your XML, it is hard to recommend a specific approach; 在不了解您的XML的情况下,很难推荐一种特定的方法。 if applicable, do an initial parsing into a database, so that you can then easily receive individual values from the database without additional parsing. 如果适用,请对数据库进行初始解析 ,以便您可以轻松地从数据库接收单个值,而无需进行其他解析。

Extending Application is one solution. 扩展Application是一种解决方案。 You can use this example. 您可以使用示例。

I decided for the SQLite database. 我决定使用SQLite数据库。 I read many discussions about the Singleton and some was negative (hard to test, some troubles with synchronization). 我阅读了很多有关Singleton的讨论,有些是负面的(难以测试,有些同步麻烦)。 So to avoid the problems I will implement parsing xml file into DB. 因此,为避免这些问题,我将实现将xml文件解析为DB。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM