App crashing when downloading a large file - NSFileHandle seekToEndOfFile

*爱你&永不变心* 提交于 2019-12-25 02:18:20

问题


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.

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.

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?

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

回答1:


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

EDIT: example subclassed NSURLConnection

@interface FileURLConnection: NSURLConnection

@property (nonatomic, strong) NSFileHandle *file;

@end


来源:https://stackoverflow.com/questions/21986815/app-crashing-when-downloading-a-large-file-nsfilehandle-seektoendoffile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!