Asynchronous data attach to file in ios?

I want to download images from a network path. But since there is a risk of large files, I thought it would be nice to write / add partial parts of NSdata directly to a file in the document directory. Is it possible? I have an asynchronous URLConnection installed. He addresses the delegate

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

method. In this method, I would like to add the so-loaded “data” to the file directly to disk. Can you help a little? If it were a butt without a low C level, that would be fine.

+3
source share
1 answer

I have never tried this, but from this apple document I suggest

@property (nonatomic,strong) NSFileHandle *handle;


//somewhere in a init or viewDidLoad
self.handle = [NSFileHandle fileHandleForUpdatingAtPath:filePath]

-(void)connection:(NSURLConnection *)connection 
   didReceiveData:(NSData *)data
{
    [handle seekToEndOfFile];
    [handle writeData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [self.handle closeFile]; 
} 

Note. filePath must be a valid path for an existing file, there are many messages related to creating the file.

+7
source

All Articles