Reputation: 5644
My app is currently parsing CSV files from a web service by using a combination of componentsSeparatedByCharactersInSet
and componentsSeparatedByString
methods.
As the files are quite large (> 1 mb on average), parsing takes a couple of seconds on an iPad, which is too slow. The memory footprint of my solution is an issue too (I am holding the full text file in memory).
This is why I am looking for a faster and more memory-efficient solution. I came across CHCSVParser
which can parse NSInputStreams
directly, e.g.
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:file];
CHCSVParser * p = [[CHCSVParser alloc] initWithInputStream:stream
usedEncoding:&encoding delimiter:';'];
(Source from the sample project on CHCSVParser)
My question:
How can I get an NSInputStream
as the result of an NSURLRequest
? (Currently I am getting the whole CSV file as a NSData
object and converting it to NSString
in order to parse it).
Could I use the NSInputStream
from an NSURLRequest
directly with CHSVParser
?
Would you generally recommend using CHCSVParser
s initWithInputStream
method with a NSURLRequest
or rather download the document to memory and parse if after the full download?
Upvotes: 0
Views: 424
Reputation: 1131
Download the file to disk using NSURLConnectionDelegate and NSOutputStream (that way you use as little memory as possible while downloading) and then open an NSInputStream to the same file and pass it into CHCSVParser.
Upvotes: 2