2012-08-17 93 views
1

目前,我嘗試下載一些文件與AFNetworking,相對較小的文件,這似乎工作,但我想一個較輕較大的文件(17MB),它似乎只是崩潰,沒有任何錯誤。AFNetworking無法下載大文件

的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]; 
} 
+0

什麼樣的碰撞?是'EXC_BAD_ACCESS'?你嘗試了啓用殭屍對象。 – 2012-08-17 09:47:08

+0

那麼它可能是內存的問題,您可以與outstream屬性設置的一個[文件流]試試[AFURLConnectionOperation(http://afnetworking.org/Documentation/Classes/AFURLConnectionOperation.html)(https://開頭開發商.apple.com /庫/的iOS /#文檔/可可/概念/流/用品/ WritingOutputStreams.html#// apple_ref/DOC/UID/20002274-BAJCABBC)。同樣創建一個'AFHTTPClient'實例,並且只使用它來爲你創建'NSURLRequest'就有點過分了。 'AFHTTPClient'意味着一個REST api客戶端,一個實例被所有的調用共享。 – rckoenes 2012-08-17 09:47:14

+0

@ bitmapdata.com,它沒有給出太多的信息,只是線程1:信號SIGTRAP,我有殭屍對象啓用。 – 2012-08-17 09:49:25

回答

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]; 
    } 
} 
+0

我看,這是一個想法我有太多,只需要創建一個定時器和更新手動,另一種想法是隻更新它的每一個5%。 但是,非常感謝,不要以爲我自己就會明白這一點 – 2012-08-17 10:12:26