gpt4 book ai didi

iphone - AFNetworking无法下载大文件

转载 作者:行者123 更新时间:2023-12-01 18:27:35 24 4
gpt4 key购买 nike

我目前正在尝试使用AFNetworking下载一些文件,对于较小的文件来说,这似乎可行,但是我正在尝试使用稍大的文件(17MB),它似乎崩溃了,没有任何错误。

网址链接到本地​​文件:http://test.local/wp-content/uploads/2012/07/test.pdf(我正在模拟器中运行它,因此可以访问)

我得到的唯一输出是在进度块中

进度:0.009022

当我检查文件系统时,似乎文件在那里,但只有几个kb。

这是AFNetworking的已知错误,还是我只是做错了什么。

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

最佳答案

以前我遇到过类似的问题。尝试解决此问题,耗时近两个星期。

您遇到了同样的问题。所以很高兴终于见到你:)

这是解决方案。

如果在downloadProgress块中直接更新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];
}
}

关于iphone - AFNetworking无法下载大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12003335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com