我有一个应用程序循环遍历一系列URL并下载该数组中的所有文件(10个小文本文件和1个大图像文件)。我有应用程序下载文件确定之前,但由于大图像的问题,我不得不继承NSURLConnection,我认为我没有完全正确,因为现在没有文件下载。
对于数组的每个项目,getFiles将传递一个URL并运行:
-(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];
}
}
以下是NSURLConnection委托:
-(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];
}
编辑:如果我在getFile中NSLog了receivedData(NSDictionary),它会更新'tag'对象,但没有数据可以使用它:
"accomodation.txt" = <>;
"future-congress-dates.txt" = <>;
"general-file.txt" = <>;
...
我还尝试为每个委托NSLog dataForConnection(NSMutableData)但是没有任何内容被打印出来:
NSLog(@"dataForConnection %@", dataForConnection);
答案 0 :(得分:1)
好的,我投降了。 有完整的代码:
FileURLConnection:
@interface FileURLConnection: NSURLConnection
@property (nonatomic, strong) NSFileHandle *file;
@end
getFile函数:
-(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);
}
原始问题:App crashing when downloading a large file - NSFileHandle seekToEndOfFile