Reputation: 607
I have an app that loops through an array of URLs and downloads all the files in that array (10 small txt files and 1 large image file). I had the app downloading the files ok before but due to a problem with the large image, I had to subclass NSURLConnection and I think I haven't quite got it right as no files are now being downloaded.
For each item of the array getFiles is passed a URL and run:
-(void)getFile:(NSString*)url tag:(NSString*)tag {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
FileURLConnection *connection = [[FileURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES tag:tag];
if (connection) {
[receivedData setObject:[NSMutableData data] forKey:connection.tag];
}
}
Here are the NSURLConnection delegates:
-(NSMutableData*)dataForConnection:(FileURLConnection*)connection {
NSMutableData *data = [receivedData objectForKey:connection.tag];
return data;
}
- (void)connection:(FileURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection*)connection];
[dataForConnection setLength:0];
}
- (void)connection:(FileURLConnection *)connection didReceiveData:(NSData *)data {
NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection*)connection];
[dataForConnection appendData:data];
}
- (NSCachedURLResponse *)connection:(FileURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(FileURLConnection *)connection {
NSLog(@"connectionDidFinishLoading run");
NSMutableData *dataForConnection = [self dataForConnection:(FileURLConnection *)connection];
NSString *fileName = connection.tag;
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[file seekToEndOfFile];
[file writeData:dataForConnection];
[file closeFile];
}
EDIT: If I NSLog the receivedData (NSDictionary) in getFile it updates the 'tag' object but no data to go with it:
"accomodation.txt" = <>;
"future-congress-dates.txt" = <>;
"general-file.txt" = <>;
...
I have also tried to NSLog the dataForConnection (NSMutableData) for each delegate but nothing ever gets printed:
NSLog(@"dataForConnection %@", dataForConnection);
Upvotes: 1
Views: 303
Reputation: 4586
Ok, I surrender. There is full code:
FileURLConnection:
@interface FileURLConnection: NSURLConnection
@property (nonatomic, strong) NSFileHandle *file;
@end
getFile function:
-(void)getFile {
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fullURL]];
FileURLConnection *conn = [[FileURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
- (void)connection:(FileURLConnection*)connection didReceiveResponse:(NSURLResponse *)response {
NSString *fileName = [[response URL] lastPathComponent];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
connection.file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
}
- (void)connection:(FileURLConnection *)connection didReceiveData:(NSData *)data {
[connection.file writeData:data];
}
- (NSCachedURLResponse *)connection:(FileURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {
return nil;
}
- (void)connectionDidFinishLoading:(FileURLConnection *)connection {
NSLog(@"Connection is %@", connection);
[connection.file closeFile];
}
- (void)connection:(FileURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"error - %@", error);
}
Original question: App crashing when downloading a large file - NSFileHandle seekToEndOfFile
Upvotes: 1