1

現在、AFNetworking を使用していくつかのファイルをダウンロードしようとしています。比較的小さなファイルの場合はこれでうまくいくようですが、少し大きなファイル (17MB) を試してみると、エラーなしでクラッシュするようです。

URL はローカル ファイルにリンクしています: 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];
}
4

1 に答える 1

4

私は以前、あなたの問題と同様の経験をしました。この問題の解決を試みてください。ほぼ 2 週間以上かかりました。

同じ問題があなたにもありました。だから私は最後にあなたに会えてうれしいです:)

ここに解決策があります。

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];
    }
}
于 2012-08-17T10:10:57.717 に答える