Pages

Wednesday, January 18, 2012

How to download a large file with the iPhone SDK and avoid memory usage issues?


I was facing some problem while downloading the large content:
Using the NSURLConnection class to download a large file in an iPhone application, but it crashes every so often because it’s using too much memory. I’m doing the usual NSURLConnection usage, to append the received data to a NSMutableData object.
Solution to the above problem is:
In the delegate method connectionDidReceiveData, we can store the content in the file
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{     
 filename = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:save_name];     
 NSFileHandle *file1 = [NSFileHandle fileHandleForUpdatingAtPath: filename];     
 [file1 writeData: data];    
 [file1 closeFile]; 
}

No comments:

Post a Comment