Sylvain Ollivier
Sylvain Ollivier

Reputation: 133

How to accelerate downloads using Cocoa API

I'm working on a simple download manager wrote in Cocoa and actually I use NSURLDownload to do the trick. The problem is that I guess NSURLDownload download the file from only one segment and I know that other download managers download files for many segments in order to improve download performance.

In fact, I think I have to split the file I want to download into multiple segments and simultaneously downloads all these segments.

The problem is that I don't know how to do it. I already search a lot in the internet without any success.


Edit : Here is a working solution thanks nacho4d help:

NSURL *downloadURL = [NSURL URLWithString:@"http://www.urltodownload.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:downloadURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 1000.0];
int fromByte = 0;
int toByte = 1000;
NSString *range = [NSString stringWithFormat:@"bytes=%i-%i", fromByte, toByte];
[request setValue:range forHTTPHeaderField:@"Range"];
[[NSURLDownload alloc] initWithRequest:request delegate:self];

Upvotes: 3

Views: 482

Answers (1)

nacho4d
nacho4d

Reputation: 45118

I don't know the exact answer for this but if you see at this Java example, you will see you can add a "Range" property in the HTTPConnection object to select the part to download:

// Specify what portion of file to download.
connection.setRequestProperty("Range",
        "bytes=" + downloaded + "-");

I think that is a hint and maybe there is a place in the NSURLRequest or NSURLConnection, etc to put this "Range" property :)

Assuming that is possible, I think you will need to make simultaneous n connections and put everything together when all connections are finished.

If you find the exact answer I would be glad to know too :)

Hope it helps.

Upvotes: 1

Related Questions