2012-06-20 50 views
6

Tôi cần tải xuống tệp> 500 Mo với AFNetworking. Đôi khi, thời gian tải xuống là> 10 phút và nếu ứng dụng ở chế độ nền, quá trình tải xuống không thể hoàn tất.AFNetworking + tệp tải xuống lớn + tiếp tục tải xuống

Vì vậy, tôi muốn thử tải xuống một phần. Tôi tìm thấy rất nhiều liên kết và điều này có vẻ là có thể với phương thức pause() và resume() trên AFHTTPRequestOperation.

Thực ra, tôi đã làm:

[self.downloadOperation setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{ 
    // Clean up anything that needs to be handled if the request times out 
    [self.downloadOperation pauseDownload]; 
    }]; 

DownloadOperation là một lớp con của AFHTTPRequestOperation (singleton).

Và trong appdelegate:

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    // resume will only resume if it's paused... 
    [[DownloadHTTPRequestOperation sharedOperation] resumeDownload]; 
} 

Máy chủ là OK để có được những dòng sản phẩm mới trong tiêu đề ...

Câu hỏi của tôi:

1) Là-t cách tốt để làm nó? 2) Tiếp tục có cần thay đổi outputStream (chắp thêm: NO => chắp thêm: YES) không? Hay nó được quản lý ở đâu đó bằng AFNetworking? (Không tìm thấy)

self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES]; 

Something như thế này (trong DownloadHTTPRequestOperation):

- (void)pauseDownload 
{ 
    NSLog(@"pause download"); 
    [self pause]; 
} 

- (void)resumeDownload 
{ 
    NSLog(@"resume download"); 
    self.outputStream = [NSOutputStream outputStreamToFileAtPath:self.filePath append:YES]; 
    [self resume]; 
} 

Nhờ sự giúp đỡ của bạn.

Trả lời

1

Tôi đã sử dụng khung công tác cũ (không ARC) ASIHTTPRequest cho một tác vụ tương tự. AllowResumeForFileDownloads thực hiện những gì bạn cần. Lưu ý rằng máy chủ của bạn cần hỗ trợ tiếp tục bằng cách đọc tiêu đề http: // Phạm vi.

if (![[NSFileManager defaultManager] fileExistsAtPath:downloadPath]){ 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request setAllowResumeForFileDownloads:YES]; 
    [request setDownloadDestinationPath:downloadPath]; 
    [request setTemporaryFileDownloadPath:tmpPath]; 
    [request startAsynchronous]; 
} 
+0

BTW AFNetworking cũng là "phi ARC" – pahan

+1

AFNetworking là ARC kích hoạt ngay bây giờ. – tangqiaoboy

Các vấn đề liên quan