AFNetworking无法下载大文件 [英] AFNetworking fails to download large files
问题描述
我目前正在尝试使用AFNetworking下载一些文件,对于相对较小的文件,这似乎有效,但我正在尝试更小的文件(17MB),它似乎只是崩溃而没有任何错误。
I'm currently trying to download some files with AFNetworking, for relative small files this seems to work, but I'm trying a slighter larger file (17MB) and it seems to just crash without any error.
网址链接到本地文件: http://test.local/wp-content/uploads/2012/07/test.pdf (我在模拟器中运行它,因此可以访问)
The url is linking to a local file: http://test.local/wp-content/uploads/2012/07/test.pdf (I'm running it in the simulator, so this is accessible)
我得到的唯一输出是在进度块中
The only output I get is in the progress block
进度:0.009022
progress: 0.009022
当我检查文件系统时,看来该文件在那里,但只有几kb。
When I check the filesystem, it appears that the file is there, but only a few kb.
这是AFNetworking的一个已知错误,或者我只是做错了。
Is this a known error with AFNetworking, or maybe I'm just doing something wrong.
- (void)downloadIssue:(Issue *)issue
{
NSString *fileName = [issue.pdf lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSURL *url = [NSURL URLWithString:issue.pdf];
AFHTTPClient *httpClient = [[[AFHTTPClient alloc] initWithBaseURL:url] autorelease];
NSURLRequest *request = [httpClient requestWithMethod:@"GET" path:issue.pdf parameters:nil];
AFURLConnectionOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"PDF DOWNLOAD COMPLETE");
issue.pdf_location = filePath;
// send out a notification with the issue
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_COMPLETE" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"PDF DOWNLOAD FAILED");
// send out a notification with the issue
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_FAILED" object:nil userInfo:[NSDictionary dictionaryWithObject:issue forKey:@"issue"]];
}];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {
float progress = (float)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"progress: %f", progress);
[[NSNotificationCenter defaultCenter] postNotificationName:@"PDF_DOWNLOAD_PROGRESS" object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys: issue, @"issue", progress, @"progress", nil]];
}];
[_queue addOperation:operation];
}
推荐答案
我以前有类似的经历因为你有问题。尝试解决这个问题,它花了差不多两个星期。
I previously had a similar experience as you problem. Try to resolve this issue, it took over almost two weeks.
同样的问题与你有关。所以我很高兴终于见到你了:)
The same problem was with you. So I'm glad to finally meet you :)
这是解决方案。
如果在downloadProgress块中直接更新UI,有时会发生未知错误。因为暧昧,但我认为主线程崩溃。所以,downloadProgress Block只更新变量,使用变量执行定时器更新UI。
if in downloadProgress block directly update UI, sometimes occur unknown error. because Ambiguous, but i think Main Thread crash. so, downloadProgress Block only update variable, execute timer update UI using a variable.
//At the same time download
myTimer = [NSTimer timerWithTimeInterval:0.1f target:self selector:@selector(updateProgress:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:processTimer forMode:NSRunLoopCommonModes];
[operation setDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead)
{
//global ivar
_progress = (float)totalBytesRead / totalBytesExpectedToRead;
}];
- (void)updateProgress:(NSTimer *)timer
{
myProgressView.progress = _progress;
if(fabs(1.f-_progress)<0.01f)
{
[timer invalidate];
}
}
这篇关于AFNetworking无法下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!