开发者

iOS: Download, unwrap, decode, and parse a large file

A project I'm working on (iPhone/Obj-C) requires me to fetch a large file (via HTTP Post) and process it. The server will return some XML wrapping BASE64 encoded gzipped XML data. ie: SERVER -> XML -> BASE64 -> GZIP -> XML -> My Model

The amount of data will vary, but I'm told the final XML will be about 5 MB.

I'd like to unwrap, decode, and parse the data as it arrives.

I'm looking for tips / pointers. (Ideally, there's existing published co开发者_StackOverflow中文版de out there, but I didn't see "stream friendly" examples in my searching.)

Will I end up subclassing NSStream?

The ideal solution will work for devices running iOS 3.2 and later.

Thanks!


Have the server (Apache?) do the gzip within the HTTP, and the iOS NSURLConnection will un-gzip as it goes. HTTP can contain binary data, so Base64 is not needed either. You should be able to get XML to arrive at your NSURLConnection as NSData, which you could feed into a SAX type parser (which can parse as it downloads).

If your server is under your control, and the server is only being used by an iOS app, and performance is your main concern, you could attempt to send your model data encoded as a binary plist. XML or JSON is probably going to be easier to work with though.


Well, this is not the answer to the question I asked, but perhaps a "solution".

The data I'm downloading is fitting nicely in memory, so there is no pressing need to optimize things to process as a stream.

  1. I use the fantastic ASIHTTPRequest library (Ben Copsey) to fetch the initial XML and just run it through an NSXML parser to grab the tag. I highly recommend ASI-HTTP-REQUEST for anyone using the HTTP protocol for iOS.

  2. Next I used a slightly tweaked (to rid clang warnings) version of Matt Gallagher' Base64 category to unwrap the Base64 to gzip.

  3. Then I run the gzip data through ASI's decoder: NSData* xmlData = [ASIDataDecompressor uncompressData:gzippedData error:&error]; to get at the XML that should have been sent all along.

  4. Finally, I run the XML through another NSXMLParser to pick out the bits of data I need.

In another part of the project, I'm actually directed to fetch a ZIP archive containing a few hundred tiny .txt files. (Yeah, it's that kind of gig.) To decode the ZIP file; I'm currently using ZipKit by Karl Moskowski.

I hope the data never grows to the point where I'll need to process it all as a stream. If it does, I know an easy way to shave off 33%. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜