1

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:

  • Reproduced on iOS 6 and 7 with XCode 5
  • All NewsstandKit methods invoked on the main thread
  • Same thing when simulating offline mode with Charles proxy (app in foreground)
Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50
  • I noticed this issue too, with iOS7. This is annoying because you cannot trust the downloaded data and you must test its integrity before any download. I think we should open a radar. – viggio24 Sep 30 '13 at 19:00
  • I was checking it on iOS 6 too - same thing – Maciek Czarnik Sep 30 '13 at 19:02
  • Yes, the problem can be reproduced on both ios6 and 7 when the SDK 7 + Xcode 5 is used. I don't remember this issue with the old SDK. I don't know how the new SDk can affect older iOS6 installations indeed. – viggio24 Sep 30 '13 at 19:16
  • Just for sanity check, did you try downloading the file from the URL using curl? Or can you try downloading it with a regular NSURLRequest? – Enrico Susatyo Oct 01 '13 at 12:41
  • Yes, downloading with NSURLConnection is ok. Simulating the offline results in `connection:didFailWithError:` – Maciek Czarnik Oct 01 '13 at 14:16

1 Answers1

0

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

    ...
}
Maciek Czarnik
  • 5,950
  • 2
  • 37
  • 50