Vojto
Vojto

Reputation: 6949

How should I store and use my 500kB data file in my iPhone app?

So I have 0.5MB XML file with data for my iPhone application. It's all read-only so I don't have to bother with writing.

Currently I'm using NSXMLParser to load data.

I had fun with NSXMLParser and I created a class that loads only the part of data that I currently need. So once the file is parsed it takes minimum of memory.

But it's very slow. It takes 500-1000ms to parse the XML file and find the part of data that I need on my iPad.

I could load all the data at once and than just use it. That would definitely save me from all the XML loading and parsing, but then I would have 500kB NSArray or something. I don't particularly like that idea.

I was wondering if there's any way to do this that wouldn't take much memory and would be fast too.

Any ideas please?

Updates:

Thanks everyone for answers. So it looks like these two options are the best for this case:

  1. Archiving XML data into binary form. It will be processed very quickly, and 500kB is really not that much to stay in the memory.
  2. If I really wanted to make it super-memory-efficient, than there's SQLite or Core Data.

I think I'm gonna try and implement number one.

Upvotes: 2

Views: 336

Answers (5)

Staeff
Staeff

Reputation: 5084

You also could use GDataXML (http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml) it supports XPath Expressions what makes it very easy to only get the parts you want.

Upvotes: 0

tc.
tc.

Reputation: 33592

Use a (binary) plist instead. There really isn't any need to overcomplicate it with CoreData/SQL/stream-based XML parsing when it all easily fits in memory.

You might get a small decrease in load time by using SQLite instead of reading the whole plist at startup, but probably at an increased access time cost, and it really isn't worth worrying about this until you hit it. If it's that big a deal, there are many simple things you can do like saving each "part" to a separate file.

Upvotes: 0

Stefan Arentz
Stefan Arentz

Reputation: 34935

500 KB really is not that much. To put it in perspective, loading a full screen image as a background on your iPad uses 2.9 MB.

So, personally I would just load all data in memory and operate it directly.

I would also convert from XML to an archived NSArray or NSDictionary. That should make the initial loading very very fast.

Upvotes: 3

Matthew Frederick
Matthew Frederick

Reputation: 22305

Sounds like the perfect case for using a database, either using SQLite directly or via Core Data. It's exactly what databases do, accessing portions of a larger data set quickly and easily.

Is there a compelling reason to leave your data in a giant XML file?

Upvotes: 5

Nathan S.
Nathan S.

Reputation: 5388

You can use standard C calls and data structures to read and process a file. So, anything you would do to make things efficient in C will work just fine.

Upvotes: 0

Related Questions