didReceiveData only getting called once on resuming download

Discussion in 'iPhone/iPad Programming' started by NigelCollins, Jan 25, 2013.

  1. NigelCollins, Jan 25, 2013
    Last edited by a moderator: Jan 25, 2013

    macrumors newbie

    Joined:
    Jan 25, 2013
    #1
    I have a problem which I could really do with some help with please

    I am using NSURLconnection to download a large file (27MB). the code work fine when there is no network interruption. In order to allow for network issues and only partially downloaded file I have added code to check to see how much of the file is downloaded and then using a server request to download the missing portion.

    The code works as it should IF I download part of file, stop the program running and then run again - the download then commences where it left off and i have complete file.

    However if I hit the download button a second time without stopping the program then didReceiveData only gets called once and adds just 200KB to the file and it tells me file has been succesfully downloaded.

    Help please - I have spent ages trying to figure out what I'm doing wrong.

    Code below:

    Code:
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {
    
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    
        NSLog(@"Response code = %d",httpResponse.statusCode );
         file = [NSFileHandle fileHandleForUpdatingAtPath:filename] ;// file is in .h
    
    
        if (file)   {
           [file seekToEndOfFile];
        }
    
    }
    
    
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
    
        if (file)  {
    
            [file seekToEndOfFile];
            NSLog(@"file is %@",file);
    
        }
    
        [self.file writeData:data];
     }
    
    
    
    
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
    
        if([[NSFileManager defaultManager] fileExistsAtPath:filename])
        {
           [file closeFile];
            file = nil;
            theConnection = nil;
            filename = nil;
            theRequest = nil;
         }
         NSLog(@"Connection failed! Error - %@ %@",
         [error localizedDescription],
         [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    }
    
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
    
        [file closeFile];
        file = nil;
        theConnection = nil;
        filename = nil;
    
    }
    
    
    - (IBAction)downloadFile:(id)sender {
    
    
        filename = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:movie1]; // filename is in .h file
    
        theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:movieDownload1]                                          cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    
        NSUInteger downloadedBytes = 0;
        NSFileManager *fm = [NSFileManager defaultManager];
        if ([fm fileExistsAtPath:filename]) {
            NSError *error = nil;
            NSDictionary *fileDictionary = [fm attributesOfItemAtPath:filename error:&error];
            if (!error && fileDictionary)
            downloadedBytes = [fileDictionary fileSize];
        } else {
            [fm createFileAtPath:filename contents:nil attributes:nil];
        }
    
        if (downloadedBytes > 0) {
             NSString *requestRange = [NSString stringWithFormat:@"bytes=%d-",downloadedBytes];
            [theRequest setValue:requestRange forHTTPHeaderField:@"Range"];
        }
    
        theConnection = nil;
        theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
    
       }
     
  2. macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    My best guess is that the delegate is getting lost on the resume. I would check that first.
     

Share This Page