如何更改 NSURLConnection 的优先级? [英] How to change NSURLConnection's priority?
问题描述
我正在开发一个音乐应用,它使用 ASIHTTPRequest
访问服务器的 api,并使用 NSURLConnection
下载音乐文件.
I'm working on a music app, which use ASIHTTPRequest
to access to server's api, and use NSURLConnection
to download music files.
一个音乐文件是 10M 左右.下载音乐文件时,访问服务器的api会比不下载音乐文件慢很多.
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.
所以我想降低下载连接的优先级.但是没有 API 可以改变 NSURLConnection
或 NSURLRequest
的优先级.
So I want to change download connection's priority lower. But there is no API to change priority of NSURLConnection
or NSURLRequest
.
如何存档?
推荐答案
我认为只有 NSOperationQueues 可以被优先处理.这是一个示例的链接:http://eng.pulse.me/tag/nsurlconnection/
I think only NSOperationQueues can be priorized. Here is a link to an example: http://eng.pulse.me/tag/nsurlconnection/
另一种方法是停止下载音乐文件,并在其他下载完成后继续下载.为此,您可以告诉 ASIHTTP 将下载保存到文件中,并根据请求继续下载.
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];
}
这篇关于如何更改 NSURLConnection 的优先级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!