a long long file reading - iphone

I want to read one text file and perform some action on that data.
file size is 67 mb
how can i read.
file is in text format.
its working in simulateor but giving memory warning in device and crashes.
code is
NSString *content = [[[NSString alloc] initWithContentsOfFile:fileName usedEncoding:nil error:nil] autorelease];
crashes when this sentence complete.
Thanks,
Shyam parmar

You haven't given code, but if you are using stringWithContentsOfFile to get an entire file consider using NSInputStream or stdio to read it and process or display it more incrementally.

Fasttracks, try what Peter suggested. The problem is loading it all at once, since you have about 20 MB available for your own app, i believe. If you'd use a NSInputStream, you can load it in pieces, due to which you wont fill up the entire memory at once. Also read this answer to another question: Objective-C: Reading a file line by line

Do you start reading the file from - (void) viewDidLoad? That might be the problem. Try start reading it in a different thread, like: [self performSelectorInBackground:#selector(method) withObject:nil];

Related

Streaming with MKNetworkKit on iOS

I'm trying to figure out how to make MKNetworkKit working with data from stream. I can see that some data is beeing downloaded (the indicator on status bar), but I don't have any idea what happens with that data after it's actually downloaded. I put a NSLog statement inside body of connection: didReceiveData: but it's not called during streaming. Any pointers how to fix that issue ?
Edit
Sorry my question was inaccurate. I know how to stream to a file but I need to stream to memory (NSData instance preferably). Okay it seems simple again due to NSOutputStream method initWithBytes:capacity:. And my problem is here, my stream has undefined length so there would be enormous impact on memory. I don't know what to do. My perfect solution works like this. Small chunks of data from the stream are processed having been downloaded and then they are discarded.
You could use the outputStreamToBuffer:capacity: method to create the stream.
As for the buffer, you can use a circular buffer, so that you can read from it as the stream writes to it. A great implementation (and explanation) is here.
Streaming a file download is a three line magic with MKNetworkKit.
//Create a MKNetworkOperation for the remote URL.
MKNetworkOperation *op = [self operationWithURLString:remoteURL
params:nil
httpMethod:#"GET"];
// add your output stream, in this case a file
[op addDownloadStream:[NSOutputStream outputStreamToFileAtPath:filePath
append:YES]];
// enqueue the operation to a MKNetworkEngine.
[self enqueueOperation:op];
That's it.

using finch by zoul, is loading 12MB of wavs into an array causing an erratic crash for my users at launch?

So my code works for about 90% of people. The rest get a crash at launch. I figure with all the memory I'm tying up here, this is the problem?
Maybe i shouldn't use an array as such?
for(i=60;i<80;i++){
myNewString = [NSMutableString stringWithFormat:#"%i", i ];
if (finchKeys[i]==nil)
finchKeys[i] = [[Sound alloc] initWithFile:
[[NSBundle mainBundle] URLForResource:myNewString withExtension:#"wav"]];
}
Is there something I can call to free up memory before I load my wavs?
thanks!!
I imagine you're not playing all of the wavs at once. Why not just save the wav url in your Sound class instead of the whole wav? Then you can just load each wav when you need to play it.

iPhone: Reading a string from a file is returning the wrong results

What I'm doing:
I am reading some data off a file several times while my app runs. I use the following code to do so:
NSString *dataPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"data.txt"];
NSString *data = [NSString stringWithContentsOfFile:dataPath encoding:NSStringEncodingConversionExternalRepresentation error:NULL];
NSArray *components = [data componentsSeparatedByString:#"|||||"];
The first time this is called, it works as expected - I get an array of length 5, each section containing a relevant string.
What goes wrong:
This file is never modified. Yet, when I call the same procedure a second time (or third, fourth etc) I don't get the same result. Instead, an array of length 1 is returned, containing only part of the necessary data.
Why? I can't see any reason for this to go wrong... any help is much appreciated!
Since the file is in you AppBundle this means that you can't modify this file at all.
Are you sure that, where ever this code is called, the autorelease object are retained correctly?
If you call this block of code every time you want this data, it might be an idea to save the results of the first time and use that every time. This will speed things up a bit.
Turns out that the code works fine. The problem was elsewhere in my code, where I was (accidentally) accessing protected directories. As a result, iOS blocked my app from accessing any files at all :o

ZipArchive memory problems on iPhone for large archive

I am trying to compress multiple files into a single zip archive and I am running into low memory warning. Since the complete zip file is loaded into the memory I guess that's the problem. Is there a way by which I can manage the compression/decompression better using ZipArchive so that not all the data is in the memory at once?
Thanks!
After doing some investigation on alternatives to ZipArchive I found another project called Objective-zip that seems to be a little better than ZipArchive. Here is the link:
http://code.google.com/p/objective-zip/
The API is quite simple. One thing I ran into was that in the begging I was reading data and never releasing it so if you are adding a bunch of large files to the zip file remember to release the data. Here is a little code I used:
ZipFile *zipFile = [[ZipFile alloc] initWithFileName:archivePath mode:ZipFileModeCreate];
for(NSString *path in subpaths){
NSData *data= [[NSData alloc] initWithContentsOfFile:longPath];
ZipWriteStream *stream = [zipFile writeFileInZipWithName:path compressionLevel:ZipCompressionLevelNone];
[stream writeData:data];
[stream finishedWriting];
[data release];
}
[zipFile close];
[zipFile release];
I hope this is helpful for anyone who runs into the same issue.
An easier way to deal with this is to simply change ZipArchive's method of reading the file into the NSData. Just change the following code
data = [ NSData dataWithContentsOfFile:file ];
to
data = [ NSData dataWithContentsOfMappedFile:file ];
That will cause the OS to read the file in a memory mapped way. Basically it just uses way less memory as it reads from the file as it needs to rather than loading it all into memory at once.

How can I read bytes from a file in iphone application, chunk by chunk?

I am trying to implement an iphone application which can read some fixed amount of bytes from a file and store in another file. this process will go on up to the end of the file. I am very new to iphone application so please help me on that . Is there any parent classes out there for this specific type of implementation?
I had a very large file which couldn't be read with NSData methods, so I used the following (per TechZen's suggestion for fine grain control).
NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath];
[fileHandle seekToFileOffset:offset];
NSData *data = [fileHandle readDataOfLength:length];
If you just want to copy the file, use NSFileManager's copy functions.
If you just want specific bytes in a file, you can load the file using NSData's file methods then you can get specific blocks of bytes and write them to file.
If you want more fine grain control, use NSFileHandler.
Edit01:
This page has examples for close to what you want. I don't have anything on hand.