Reputation: 3770
I have the following Problem which drives me nuts for several weeks now.
I'have a small framework for downloading files. This framework has the ability to pause and resume a filedownload.
So far so good.
Problem is, everytime i pause a download then, after i resume it, the NSURLConnection
responsible for the download will not call connectionDidFinishLoading
, if the downloadedbytes equals the expected filesize, but keeps on calling connectionDidReceiveData
and thus corrupts my download.
I have no idea why this should be. When i don't pause/resume the download, everything works fine.
Here is the code of the methods to pause and resume the download.
- (id)pause
{
[self.connection cancel];
self.connection = nil;
return self;
}
- (id)resume
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:600];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *localSavePath = self.savePath;
if (failBlocks.count > 0) {
[self cancel];
[self start];
}
else {
if( ![manager fileExistsAtPath:localSavePath] )
{
[manager createFileAtPath:localSavePath contents:[NSData data] attributes:nil];
}
if (self.downloadData.length > 0) {
log_muma2(@"Should resume url %@",self.url);
// Define the bytes we wish to download.
NSString *range = [NSString stringWithFormat:@"bytes=%i-", downloadData.length];
[request setValue:range forHTTPHeaderField:@"Range"];
}
if (!self.connection) {
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = conn;
}
}
return self;
}
I would be really glad if someone could help me out of this.
I already tested, if the already downloaded data is of the correct size and stuff like that. everything seems to be ok.
Many thanks in Advance. Maverick
=========Edit=========
Here is the code of my didReceiveData
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
amountDownloaded += data.length;
NSInteger receivedLen = [data length];
bytesReceived = (bytesReceived + receivedLen);
if(expectedSize != NSURLResponseUnknownLength) {
progress = ((bytesReceived/(float)expectedSize)*100)/100;
[self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];
percentComplete = progress*100;
}
if (self.savePath == nil || [self.savePath isEqualToString:@""]) {
[self.downloadData appendData:data];
}
else {
[self.downloadData appendData:data];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.savePath];
[handle seekToEndOfFile];
[handle writeData:data];
//
}
if (expectedSize < bytesReceived)
{
NSLog(@"download exceeded expected size %f with %lld", expectedSize, bytesReceived);
[self pause];
[self cancel];
self.connection = nil;
self.downloadData = nil;
bytesReceived = 0;
expectedSize = 0;
amountDownloaded = 0;
progress = 0;
percentComplete = 0;
[self start];
}
}
Upvotes: 3
Views: 1305
Reputation: 1836
How do you calculate your expected file size?
If you use response.expectedContentLength
, be aware that this value is reduced every time you initialize a new connection when resuming the download.
Upvotes: 2