简体   繁体   English

下载大文件时应用崩溃-NSFileHandle seekToEndOfFile

[英]App crashing when downloading a large file - NSFileHandle seekToEndOfFile

I have a loop that gets the URLs of a bunch of files (10 small txt files and 1 large image file around 700KB) and runs 'getFile' which creates an NSUrlConnection for each one. 我有一个循环,它获取一堆文件(10个小txt文件和1个大图像文件,大约700KB)的URL,然后运行“ getFile”,为每个文件创建一个NSUrlConnection。

When the app gets to [file seekToEndOfFile] just before [file writeData:data] it crashes with: 当应用程序在[file writeData:data]之前到达[file seekToEndOfFile]时,它崩溃并显示:

*** Terminating app due to uncaught exception 'NSFileHandleOperationException', reason: '*** -[NSConcreteFileHandle seekToEndOfFile]: No such process'
*** First throw call stack:

The strange thing is that if I step through the code (ie slowly allowing each connection to go and come back) then all files are downloaded fine. 奇怪的是,如果我单步执行代码(即慢慢允许每个连接前进并返回),那么所有文件都可以正常下载。 If I just let the app do its thing it crashes. 如果我只是让应用执行其操作,则它会崩溃。

Here is code for the connections: 这是连接的代码:

-(void)getFile {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fullURL]];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    [conn start];
}
- (void)connection:(NSURLConnection *)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];
    file = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
    [file seekToEndOfFile];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [file seekToEndOfFile]; // crashing here
    [file writeData:data]; 
}
- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
              willCacheResponse:(NSCachedURLResponse*)cachedResponse {
    return nil;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"Connection is %@", connection);
    [file closeFile];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"error - %@", error);
}

Does my app have a problem keeping reference to the outgoing connections? 我的应用程序在保留对传出连接的引用方面是否有问题? I had assumed that NSURLConnections, by default, were asynchronous and you would not need to 'keep track' of them? 我假设默认情况下,NSURLConnections是异步的,您不需要“跟踪”它们吗?

EDIT I have subclassed NSURLConnection and instantiated as below: 编辑我有子类化NSURLConnection并实例化如下:

-(void)getFile {
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:fullURL]];
    FileURLConnection *conn = [[FileURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES ];
    conn.fileName = [fullURL lastPathComponent];
    [conn start];
}

I think you are downloading several files simultaneously with one delegate. 我认为您正在与一位代表同时下载多个文件。 Try subclass NSURLConnection connection and add in it proterty file , instead delegate's file property. 尝试子类NSURLConnection连接,并在其中添加属性file ,而不是委托的file属性。 And I think you don't need [file seekToEndOfFile]; 而且我认为您不需要[file seekToEndOfFile];

EDIT: example subclassed NSURLConnection 编辑:子类NSURLConnection的示例

@interface FileURLConnection: NSURLConnection

@property (nonatomic, strong) NSFileHandle *file;

@end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS:NSFileHandle与NSOutputStream进行大文件下载 - iOS: NSFileHandle vs NSOutputStream for large file download 如何NSFile处理从另一个应用程序获取的文件 - How to NSFileHandle a file got from another app SIGABRT:-[NSConcreteFileHandle seekToEndOfFile]:没有这样的文件或目录 - SIGABRT: -[NSConcreteFileHandle seekToEndOfFile]: No such file or directory 为大型文件和小型文件创建NSFileHandle之间有性能差异吗? - Is there any performance difference between creating an NSFileHandle for a large versus a small file? 下载大(> 500mb)zip文件时,Appcelerator App崩溃 - Appcelerator App crashes when downloading large (>500mb) zip file iOS-仅当在设备上手动运行应用程序时,NSFileHandle availableData才会挂起 - iOS - NSFileHandle availableData hangs only when app is run manually on device 从URL显示大尺寸图像时iOS App崩溃 - iOS App is crashing when display large size image from URL 下载大文件时如何清除 NSURLSession 的缓存? - How to clear cache of NSURLSession when a large file is downloading? 背景崩溃时应用崩溃 - App crashing when backgrounded 应用程序崩溃。 当我从自定义表格单元格下载视频的第二个索引时 - app is crashing. when i'm downloading second index of video from custom table cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM