下载大文件时应用崩溃-NSFileHandle seekToEndOfFile [英] App crashing when downloading a large file - NSFileHandle seekToEndOfFile

查看:119
本文介绍了下载大文件时应用崩溃-NSFileHandle seekToEndOfFile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,它获取一堆文件(10个小txt文件和1个大图像文件,大约700KB)的URL,然后运行 getFile,为每个文件创建一个NSUrlConnection。

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.

当应用程序在[file writeData:data]之前进入[file seekToEndOfFile]时,它崩溃并显示:

When the app gets to [file seekToEndOfFile] just before [file writeData:data] it crashes with:

*** 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 (i.e. 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.

以下是连接的代码:

-(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);
}

我的应用程序在保留对传出连接的引用方面是否有问题?我以为默认情况下NSURLConnections是异步的,您不需要跟踪它们?

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?

编辑 NSURLConnection并实例化如下:

EDIT I have subclassed NSURLConnection and instantiated as below:

-(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];
}


推荐答案

我认为您正在下载多个与一位代表同时提交文件。尝试使用子类 NSURLConnection 连接,并在其中添加属性 file ,而不是委托文件属性。而且我认为您不需要 [file seekToEndOfFile];

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. And I think you don't need [file seekToEndOfFile];

编辑:
示例继承了NSURLConnection

example subclassed NSURLConnection

@interface FileURLConnection: NSURLConnection

@property (nonatomic, strong) NSFileHandle *file;

@end

这篇关于下载大文件时应用崩溃-NSFileHandle seekToEndOfFile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆