다음과 같은 문제가 발생하여 몇 주 동안 견뎌 낼 수 있습니다.파일을 다운로드 할 때 NSURLConnection이 파일 끝에서 didFinishLoading을 호출하지 않습니다. 다운로드가 일시 중지되었다가 다시 시작될 때
파일 다운로드를위한 작은 프레임 워크가 있습니다. 이 프레임 워크는 파일 업로드를 일시 중지하고 다시 시작할 수 있습니다. 지금까지 너무 좋아. 문제는 다운로드를 일시 중지 할 때마다 다운로드를 담당 한 NSURLConnection
이 connectionDidFinishLoading
을 호출하지 않고 다운로드 된 바이트가 예상 파일 크기와 같지만 계속 connectionDidReceiveData
을 호출하여 다운로드를 손상시킵니다. 나는 이것이 왜 있어야하는지 잘 모른다. 내가 일시 중지/다운로드를 재개하지 않으면 모든 것이 잘 작동합니다. 다음은 다운로드를 일시 중지하고 다시 시작하는 방법의 코드입니다.
- (id)pause
{
[self.connection cancel];
self.connection = nil;
return self;
}
- (id)resume
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:600];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *localSavePath = self.savePath;
if (failBlocks.count > 0) {
[self cancel];
[self start];
}
else {
if(![manager fileExistsAtPath:localSavePath])
{
[manager createFileAtPath:localSavePath contents:[NSData data] attributes:nil];
}
if (self.downloadData.length > 0) {
log_muma2(@"Should resume url %@",self.url);
// Define the bytes we wish to download.
NSString *range = [NSString stringWithFormat:@"bytes=%i-", downloadData.length];
[request setValue:range forHTTPHeaderField:@"Range"];
}
if (!self.connection) {
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = conn;
}
}
return self;
}
누군가 나를 도울 수 있다면 정말 기쁠 것입니다.
이미 다운로드 한 데이터의 크기와 크기가 이미 맞으면 이미 테스트를 마쳤습니다. 모든 것이 괜찮은 것 같습니다.
사전 감사드립니다. 코디
========= 편집 ========= 여기
당신이 당신의 예상 파일을 계산 어떻게 내 didReceiveData
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
amountDownloaded += data.length;
NSInteger receivedLen = [data length];
bytesReceived = (bytesReceived + receivedLen);
if(expectedSize != NSURLResponseUnknownLength) {
progress = ((bytesReceived/(float)expectedSize)*100)/100;
[self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];
percentComplete = progress*100;
}
if (self.savePath == nil || [self.savePath isEqualToString:@""]) {
[self.downloadData appendData:data];
}
else {
[self.downloadData appendData:data];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.savePath];
[handle seekToEndOfFile];
[handle writeData:data];
//
}
if (expectedSize < bytesReceived)
{
NSLog(@"download exceeded expected size %f with %lld", expectedSize, bytesReceived);
[self pause];
[self cancel];
self.connection = nil;
self.downloadData = nil;
bytesReceived = 0;
expectedSize = 0;
amountDownloaded = 0;
progress = 0;
percentComplete = 0;
[self start];
}
}
Thx. 그것은 실제로 내 문제를 해결했습니다. 더블 체크를하지 않고 코드를 가져 가면 이런 일이 발생합니다. – Maverick1st