albertodebortoli/GoldRaccoon

Stop / Resume download feature

Opened this issue · 0 comments

Hi,
I'm trying to modify the code to support stop/resume download feature. In my app I can stop the download, save the partial data to a file. Then I want to be able to restart the operation and get the rest of data from the remote file using an offset.

In GRStreamInfo.m I added

if (request.readOffset > 0)
{
CFReadStreamSetProperty(readStreamRef, kCFStreamPropertyFileCurrentOffset, (CFNumberRef)[NSNumber numberWithUnsignedLongLong:request.readOffset]);
}

before
self.readStream.delegate = request;
[self.readStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[self.readStream open];

I've added readOffset as a property in GRRequest and the setup looks like this

unsigned long long size = [self.fileHandle seekToEndOfFile]; //can be 0 if the file is empty or > 0 if some data was already downloaded
self.downloadRequest = [[GRDownloadRequest alloc] initWithDelegate:self datasource:self];
self.downloadRequest.path = @"path_to_the_file";
self.downloadRequest.localFilePath = @""; //i don't use this
self.downloadRequest.passiveMode = NO;
self.downloadRequest.readOffset = (unsigned int)size;
[self.downloadRequest start];

But the download always start from the beginning of the remote file. Well, I'm not sure if this is supposed to work anyway (from the docs of the NSStream: "By default, NSStream instances that are not file-based are non-seekable, one-way streams (although custom seekable subclasses are possible). Once the data has been provided or consumed, the data cannot be retrieved from the stream"). Anyone care to comment on this issue? Am I doing some wrong, is there something missing in order to make it work?