Iphone 下载文件时,当下载暂停并继续时,NSURLConnection不会在文件末尾调用didFinishLoading

Iphone 下载文件时,当下载暂停并继续时,NSURLConnection不会在文件末尾调用didFinishLoading,iphone,objective-c,ios,download,nsurlconnection,Iphone,Objective C,Ios,Download,Nsurlconnection,我有以下问题,这让我发疯好几个星期了 我有一个下载文件的小框架。该框架能够暂停和恢复文件下载。 到现在为止,一直都还不错。 问题是,每次我暂停下载,然后在我恢复下载后,负责下载的NSURLConnection不会调用connectionidfinishload,前提是下载的字节数等于预期的文件大小,而是继续调用connectionidreceivedata,从而破坏我的下载。 我不知道为什么会这样。当我不暂停/恢复下载时,一切正常。 以下是暂停和恢复下载的方法代码 - (id)pause {

我有以下问题,这让我发疯好几个星期了

我有一个下载文件的小框架。该框架能够暂停和恢复文件下载。 到现在为止,一直都还不错。 问题是,每次我暂停下载,然后在我恢复下载后,负责下载的
NSURLConnection
不会调用
connectionidfinishload
,前提是下载的字节数等于预期的文件大小,而是继续调用
connectionidreceivedata
,从而破坏我的下载。 我不知道为什么会这样。当我不暂停/恢复下载时,一切正常。 以下是暂停和恢复下载的方法代码

- (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;    
}
如果有人能帮我摆脱困境,我会非常高兴

我已经测试过了,如果已经下载的数据大小正确之类的话。一切似乎都很好

非常感谢。 特立独行的

==========编辑=========

这是my
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];
    }
}
-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
{
amountDownloaded+=data.length;
NSInteger receivedLen=[数据长度];
bytesReceived=(bytesReceived+receivedLen);
if(预期大小!=NSURResponseUnknownlength){
进度=((字节接收/(浮动)预期大小)*100)/100;
[self-performSelectorOnMainThread:@selector(updateProgressBar),对象:nil waitUntilDone:NO];
完成百分比=进度*100;
}
if(self.savePath==nil | |[self.savePath isEqualToString:@”“){
[self.downloadData:data];
}
否则{
[self.downloadData:data];
NSFileHandle*handle=[nsfilehandlefilehandleforwritingatpath:self.savePath];
[handle seekToEndOfFile];
[句柄写入数据:数据];
//        
}    
如果(预期大小<收到字节数)
{
NSLog(@“下载超过预期大小%f,有%lld”,预期大小,字节数已收到);
[自我暂停];
[自动取消];
自连接=零;
self.downloadData=nil;
字节数=0;
expectedSize=0;
下载量=0;
进度=0;
完成百分比=0;
[自启动];
}
}

如何计算预期的文件大小


如果使用
response.expectedContentLength
,请注意,在恢复下载时,每次初始化新连接时,此值都会减小。

Thx。这确实解决了我的问题。当您只获取一些代码而不进行双重检查时,就会发生这种情况。
- (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;    
}