NSURLResponse returns Content-Range, ignoring the range I requested

When downloading a file, I want to resume downloading. For instance:

NSFileManager *fm = [NSFileManager defaultManager]; NSString *downloadTempPath = [_tempPath path]; if ([fm fileExistsAtPath:downloadTempPath]) { // We have a partial download. Get the rest of the file starting at byteCount NSDictionary *dAttrib = [fm attributesOfItemAtPath:downloadTempPath error:nil]; NSUInteger byteCount = [dAttrib fileSize]; NSLog(@"Preparing to resume download: %@", downloadTempPath); NSString *requestRange = [NSString stringWithFormat:@"bytes=%d-", byteCount]; [self.request setValue:requestRange forHTTPHeaderField:@"Range"]; } 

This will create a receive request with a header like this:

Range: bytes = 1000-

I would expect the first answer to 206 to give me a piece where I asked him to start, having a header and a value similar to this (unloaded from allHeaderFields ):

"Content-Range" = "bytes 1000-340197 / 340198";

But sometimes it jumps ahead and returns something like this:

"Content-Range" = "byte 32000-340197 / 340198";

This is really bad, because I need these bytes from 1000-32000, and this did not give me.

I know that there is no server error, because when I hang on the server, it returns the section that I requested. Also, when I use wirehark, I do not see any packet going to the server. So this is a problem from iOS, possibly with NSURLCache

+4
source share
1 answer

This seems to be a bug in NSURLCache . When I discover that a problem has occurred, I do the following and try again.

  [[NSURLCache sharedURLCache] removeCachedResponseForRequest:self.request]; 

Once I have done this, the problem will not recur. He seems to be healing the cache. I saw this on iOS5.0, and since I can no longer play, I don’t know if this is happening on newer OSs.

+1
source

All Articles