Question

I'm downloading mp3 files from Rackspace cloud files, and for large files i'm encountering an issue where the download is completed successfully but the file is not yet downloaded completely. For example, a 40 MB mp3 file (01:00:00 duration) is download as as 4.5 MB mp3 file (00:10:30 duration). This doesn't happen all the time.

  1. Any pointers as to what's going on?
  2. Why is this happening, and how can i fix this issue?
  3. How can i build a simple checksum logic to check if the file was downloaded completely?

Here's how i create and send an async request:

ASIHTTPRequest *request;
request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:urlString]];
[request setShouldAttemptPersistentConnection:NO]; 
[request setAllowResumeForFileDownloads:YES];
[request setDownloadProgressDelegate:self];
[request setShouldContinueWhenAppEntersBackground:YES];
[request setUserInfo:userInfo];
[request setDownloadDestinationPath:downloadPath];
[request setTemporaryFileDownloadPath:[NSString stringWithFormat:@"%@.download", downloadPath]];

[self.networkQueue addOperation:request];
[self.networkQueue go];

Note i'm using a network queue with 4 concurrent downloads.

Thanks.

Edit (Mon March 5, 2012, 03:25 PM)

So, further investigation shows that ASINetworkQueue is calling requestDidFinishSelector delegate method instead of requestDidFailSelector. The status code returned by the ASIHTTPRequest object is 206, HTTP/1.1 206 Partial Content in requestDidFinishSelector method. The status code should be 200, HTTP/1.1 200 OK.

I still don't know why! and i still don't know how to fix this. It seems that i'll have to delete the partially downloaded file and start the download process again. At this point the temporary file i.e. %@.download is removed, and this partially downloaded file is put at the destination path.

Was it helpful?

Solution

So, this is what i ended up doing, and hopefully this'll be enough (to solve the problem).

Here's how i'm creating the network queue:

- (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;
}

And here's what my contentRequestDidSucceed: method does:

- (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!
    // . . .
}

If there's a better way to handle this, please let me know.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top