2012-09-24 2 views
4

AFNetworking을 사용하여 큰 파일을 내 iPad 앱에 다운로드하고 있습니다.AFNetworking이 다운로드를 시작하지 않습니다.

AFHTTPRequestOperation 인스턴스가이 파일을 다운로드하는 데 사용됩니다. 아래 참조 코드입니다 -

 
//request is the NSRequest object for the file getting downloaded 
AFHTTPRequestOperation *operation = [self HTTPRequestOperationWithRequest:request 
             success:^(AFHTTPRequestOperation *operation, id responseObject) {                   

             } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 


             }]; 
//here path variable is the location where file would be placed on download 
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:path 
                   append:YES]; 
//since this class is subclass of AFHTTPClient so the operation is added to request queue 
[self enqueueHTTPRequestOperation:operation]; 

이제 여기에 문제가 내가 일시 중지를 시도하고 기능 아래 사용하여이 다운로드를 재개 할 때 pauseDownload 기능이 이력서 다운로드가 예상대로 작동하지 않습니다하지만 제대로 작동하고 그것은 처음부터 다운로드가 시작되는 것처럼 보였습니다. 어디에서 시작했는지에 관해서는 그것이 떠난 곳에서 다시 시작될 것입니다. 여기에 어떤 문제가있을 수 있습니까?

 
-(void)pauseDownload{ 
    [operation pause]; 
} 

-(void)resumeDownload{ 
    [operation resume]; 
} 

답변

6

언젠가 지출을 중단 한 후 다운로드를 일시 중지하고 다시 시작하는 방법을 알아 냈습니다.

AFNetworking은 extensions 중 하나가 AFDownloadRequestOperation이며 대용량 파일의 일시 중지 및 다시 시작을 처리하는 데 필수적으로 사용됩니다. 따라서 여기 AFHTTPRequestOperation을 사용하는 대신 AFDownloadRequestOperation이 사용됩니다. 아래는 샘플 코드입니다

 
//request is the NSRequest object for the file getting downloaded and targetPath is the final location of file once its downloaded. Don't forget to set shouldResume to YES 
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request 
                        targetPath:targetPath 
                        shouldResume:YES]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
    //handel completion 
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    //handel failure 
}]; 
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) { 
    //handel progress 

}]; 
//since this class is subclass of AFHTTPClient so the operation is added to request queue 
[self enqueueHTTPRequestOperation:operation]; 

//used to pause the download 
-(void)pauseDownload{ 
    [operation pause]; 
} 
//used to resume download 
-(void)resumeDownload{ 
    [operation resume]; 
} 
+2

앱을 종료하고 다시 시작할 때도 작동합니까? – openfrog

+0

@openfrog 예 앱이 종료되어 다시 시작될 때도 작동합니다. –

+2

이 나를 도와주세요. [self enqueueHTTPRequestOperation : operation]; –

관련 문제