iosobjective-cnsurlconnectionnewsstand-kit

Newsstand and network errors


Guys I'm working on the Newsstand stuff now. I'm trying to handle network errors.

What you see on the image below is my simple log ("Percentage: %i" is inside connection:didWriteData:totalBytesWritten:expectedTotalBytes:).

My problem is depicted in the last 3 lines of code.

enter image description here

What I've done in this lines:

  1. After that line I've switched on the airplane mode (simulated network error)
  2. I've received connection:didWriteData:totalBytesWritten:expectedTotalBytes: with totalBytesWritten equal to expectedTotalBytes
  3. I've received connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *)destinationURL.

After that:

Hooray, I've just finished downloading my .zip, I can unpack it, announce the status to my view and so on... :(

My question is what's going on?

I have implemented connection:didFailWithError: but it's not invoked.

I was trying to grab the totalBytesWritten in last invoked didWriteData: and compare it to real file size in DidFinishDownloading:

I have stripped all my project away just to make sure that its not related to my whole design.

I'm thinking about combination of NSTimer and NKIssueContentStatusAvailable to check the real download status.

It's all hacky. Isn't it?

Update:


Solution

  • It's not an issue anymore when switching to Airplane, but still can reproduce the issue when throttling on Charles proxy.

    I ended up with this solution (checking if connection:didWriteData:... is telling the truth in connectionDidFinishDownloading:destinationURL:):

    - (void)connection:(NSURLConnection *)connection didWriteData:(long long)bytesWritten totalBytesWritten:(long long)totalBytesWritten expectedTotalBytes:(long long)expectedTotalBytes
    {
        ...
        self.declaredSizeOfDownloadedFile = expectedTotalBytes;
    }
    

    And:

    - (void)connectionDidFinishDownloading:(NSURLConnection *)connection destinationURL:(NSURL *) destinationURL
    {
        NSDictionary* fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:destinationURL.absoluteString error:nil];
        NSNumber* destinationFileSize = [fileAttributes objectForKey:NSFileSize];
        if (destinationFileSize.intValue != self.declaredSizeOfDownloadedFile)
        {
            NSError* error = ...;
            [self connection:connection didFailWithError:error];
    
            self.declaredSizeOfDownloadedFile = 0;
    
            return;
        }
    
        ...
    }