AFNetworking无法下载大文件

时间:2012-08-17 09:37:36

标签: iphone ios afnetworking

我目前正在尝试使用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];
}

1 个答案:

答案 0 :(得分:4)

我之前遇到过类似的问题。试着解决这个问题,它花了差不多两个星期。

同样的问题与你有关。所以我很高兴终于见到你了:)

这是解决方案。

如果在downloadProgress块中直接更新UI,有时会发生未知错误。因为暧昧,但我认为主线程崩溃。所以,downloadProgress Block只更新变量,使用变量执行计时器更新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];
    }
}