How to change NSURLConnection's priority?

2019-07-15 12:00发布

问题:

I'm working on a music app, which use ASIHTTPRequest to access to server's api, and use NSURLConnection to download music files.

One music file is 10M or so. When downloading music file, access to server's api will be much more slow than not downloading music file.

So I want to change download connection's priority lower. But there is no API to change priority of NSURLConnection or NSURLRequest.

How to archive this?

回答1:

I think only NSOperationQueues can be priorized. Here is a link to an example: http://eng.pulse.me/tag/nsurlconnection/

An alternative would be, to stop downloading the music file an resume it after your other downloads are completed. To do this you could tell ASIHTTP to save a download to a file and resume it on request.

Here is an example from one of my projects:

- (void) executeFileDownloadAtURL:(NSURL *) aURL toDirectory:(NSString *) aDestinationPath;
{
    self.request = [ASIHTTPRequest requestWithURL:aURL];
    [self.request setDownloadDestinationPath:aDestinationPath];
    self.request.downloadProgressDelegate = self.delegate;
    [self.request setAllowResumeForFileDownloads:YES];
    NSString *tmpDir = NSTemporaryDirectory();
    NSString *tempFilePath = [NSString stringWithFormat:@"%@%@", tmpDir, @"TempMovie.mp4"];
    [self.request setTemporaryFileDownloadPath:tempFilePath];
    self.request.showAccurateProgress = YES;
    self.request.delegate = self;
    [self.request startAsynchronous];
}