Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 25, 2013, 03:46 AM   #1
NigelCollins
macrumors newbie
 
Join Date: Jan 2013
didReceiveData only getting called once on resuming download

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];

   }

Last edited by dejo; Jan 25, 2013 at 08:16 AM. Reason: Please use [code] tags.
NigelCollins is offline   0 Reply With Quote
Old Jan 28, 2013, 06:37 AM   #2
KoolStar
macrumors Demi-God
 
KoolStar's Avatar
 
Join Date: Oct 2006
Location: Kentucky
Send a message via AIM to KoolStar Send a message via Skype™ to KoolStar
My best guess is that the delegate is getting lost on the resume. I would check that first.
__________________
"Innovation distinguishes between a leader and a follower." : S.J.
KoolStar is online now   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 09:45 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC