Iphone AFNetworking无法下载大型文件
我目前正在尝试使用AFNetworking下载一些文件,对于相对较小的文件,这似乎是可行的,但我正在尝试一个更大的文件17MB,它似乎只是崩溃没有任何错误 url链接到一个本地文件:我在模拟器中运行它,所以这是可以访问的 我得到的唯一输出是进度块 进展:0.009022 当我检查文件系统时,文件似乎在那里,但只有几kb 这是AFN网络的已知错误,还是我只是做错了什么Iphone AFNetworking无法下载大型文件,iphone,ios,afnetworking,Iphone,Ios,Afnetworking,我目前正在尝试使用AFNetworking下载一些文件,对于相对较小的文件,这似乎是可行的,但我正在尝试一个更大的文件17MB,它似乎只是崩溃没有任何错误 url链接到一个本地文件:我在模拟器中运行它,所以这是可以访问的 我得到的唯一输出是进度块 进展:0.009022 当我检查文件系统时,文件似乎在那里,但只有几kb 这是AFN网络的已知错误,还是我只是做错了什么 - (void)downloadIssue:(Issue *)issue { NSString *fileName = [
- (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];
}
我以前有过和你类似的经历。为了解决这个问题,花了将近两周的时间 你也有同样的问题。很高兴终于见到你: 这是解决办法 如果在下载进度块中直接更新UI,有时会发生未知错误。因为模棱两可,但我认为主线程崩溃了。所以,downloadProgress块仅更新变量,使用变量执行计时器更新UI
//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];
}
}
我以前有过和你类似的经历。为了解决这个问题,花了将近两周的时间 你也有同样的问题。很高兴终于见到你: 这是解决办法 如果在下载进度块中直接更新UI,有时会发生未知错误。因为模棱两可,但我认为主线程崩溃了。所以,downloadProgress块仅更新变量,使用变量执行计时器更新UI
//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];
}
}
什么样的碰撞?EXC\u访问是否不好?您尝试启用僵尸对象。这可能是内存问题,您可以尝试将扩展属性设置为a。另外,创建一个AFHTTPClient实例并仅使用它为您创建NSURLRequest也有点过分。AFHTTPClient是一个REST api客户端,一个由所有调用共享的实例。@bitmapdata.com,它不提供太多信息,只提供线程1:signal SIGTRAP,我启用了僵尸对象。我以前有过与您类似的经历。请重试删除SetDownloadPrgoResBlock中的postNotification。并重新尝试:。还是不行?@rckoenes,我只是尝试只使用一个AFHTTPClient实例,但效果相同,可以下载一些文件,larget似乎崩溃了什么样的崩溃?EXC\u访问是否不好?您尝试启用僵尸对象。这可能是内存问题,您可以尝试将扩展属性设置为a。另外,创建一个AFHTTPClient实例并仅使用它为您创建NSURLRequest也有点过分。AFHTTPClient是一个REST api客户端,一个由所有调用共享的实例。@bitmapdata.com,它不提供太多信息,只提供线程1:signal SIGTRAP,我启用了僵尸对象。我以前有过与您类似的经历。请重试删除SetDownloadPrgoResBlock中的postNotification。并重新尝试:。还是不行吗?@rckoenes,我只是试着只使用一个AFHTTPClient实例,但它有同样的效果,可以下载一些文件,larget似乎很难理解,这也是我的想法,只需创建一个计时器并手动更新,另一个想法是每5%更新一次。但是非常感谢,我不认为我自己会明白这一点。我明白了,这也是我的想法,只是创建一个计时器并手动更新,另一个想法是每5%更新一次。但是非常感谢,我想我自己也不会明白的