简体   繁体   中英

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. 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.

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. 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)
  • Use lazy loading (load the part of text, which I actualy need) (I don't want use this as well)

Could someone say what would be best solution ? And describe how to do it ?

(Sorry for my english)

  • EDIT 1: xml structure:

     < 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.

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).

The main problem in regards to your application is, however, that Android is allowed to start and stop your application at will. 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; 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. You can use this example.

I decided for the SQLite database. I read many discussions about the Singleton and some was negative (hard to test, some troubles with synchronization). So to avoid the problems I will implement parsing xml file into DB.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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