2014-01-28 35 views
0

我正在使用AFHTTPRequestOperation下載文件。但是在暫停和恢復操作時,api會給出不正確的進度計數。我使用下面的代碼AFNetworking給出錯誤的進度計數

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]]; 
_downloadOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
_downloadOperation.outputStream = [NSOutputStream outputStreamToFileAtPath:strFilePath append:YES]; 

[_downloadOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 

    NSLog(@"Progress %lld",totalBytesRead * 100/totalBytesExpectedToRead); 

}]; 

[_downloadOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    NSLog(@"downloaded %@",operation.request.URL); 


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

    NSLog(@"failed"); 

}]; 
[_downloadOperation start]; 

當用戶進入後臺我暫停運行下載數據,

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    if ([objAPI.downloadOperation isExecuting]) 
     [objAPI.downloadOperation pause]; 
} 

,當用戶來到在前臺恢復運行

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    if ([objAPI.downloadOperation isPaused]) 
     [objAPI.downloadOperation resume]; 
} 

例子:如果操作在進度中暫停20%,則在恢復時從20%開始,但結束於120%。換句話說,暫停操作後,進度計數不正確。

請幫我解決這個問題

回答