所以,这就是我最终要做的,希望这足以(解决问题)。
这是我创建网络队列的方式:
- (ASINetworkQueue *)networkQueue {
if (!_networkQueue) {
_networkQueue = [[ASINetworkQueue alloc] init];
[_networkQueue setShowAccurateProgress:YES];
[_networkQueue setRequestDidFinishSelector:@selector(contentRequestDidSucceed:)];
[_networkQueue setRequestDidFailSelector:@selector(contentRequestDidFail:)];
[_networkQueue setShouldCancelAllRequestsOnFailure:NO];
[_networkQueue setDelegate:self];
}
return _networkQueue;
}
这就是我的contentRequestDidSucceed:
方法的作用:
- (void)contentRequestDidSucceed:(ASIHTTPRequest *)request {
// ASIHTTPRequest doesn't use HTTP status codes (except for redirection),
// so it's up to us to look out for problems (ex: 404) in the requestDidFinishSelector selector.
// requestDidFailSelector will be called only if there is the server can not be reached
// (time out, no connection, connection interrupted, ...)
// In certain cases ASIHTTPRequest/ASINetworkQueue calls the delegate method requestDidFinishSelector,
// instead it should call requestDidFailSelector. I've encountered this specific case with status code 206 (HTTP/1.1 206 Partial Content). In this case the file was not completely downloaded, so we'll have to re-process the request.
if ([request responseStatusCode] != 200) {
NSLog(@" ");
NSLog(@"======= BEEP =======");
NSLog(@" ");
// We're double checking that the file was indeed not downloaded completely!
// During internal testing, we encountered a case where download was successful
// but we received 206 as the response code (maybe we received the cached value).
unsigned long long progress = [request totalBytesRead] + [request partialDownloadSize];
unsigned long long total = [request contentLength] + [request partialDownloadSize];
if (progress != total) {
NSString *downloadPath = [request downloadDestinationPath];
NSString *temporaryDownloadPath = [self temporaryPathForFile:downloadPath];
// Move the file at destination path to the temporary destination path (back again)
NSError *moveError = nil;
[[[[NSFileManager alloc] init] autorelease] moveItemAtPath:downloadPath
toPath:temporaryDownloadPath
error:&moveError];
if (moveError) {
NSLog(@"Failed to move file from '%@' to '%@'", downloadPath, temporaryDownloadPath);
NSError *removeError = nil;
[ASIHTTPRequest removeFileAtPath:downloadPath error:&removeError];
if (removeError) {
NSLog(@"Failed to remove file from '%@'", downloadPath);
}
}
// Call the requestDidFailSelector method
[self contentRequestDidFail:request];
// Don't continue
return;
}
}
// TODO: Process successful request!
// . . .
}
如果有更好的方法来处理这个问题,请告诉我。