在“[NSURLSession backgroundSessionConfigurationWithIdentifier]”上设置最大 http 连接数不起作用
Setting the max http connections on `[NSURLSession backgroundSessionConfigurationWithIdentifier]` is not working
我正在尝试为我们的应用创建一个简单的下载器。该应用程序将从我们的服务器下载大文件(每个文件至少 100 MB),因此我想实现一个即使应用程序处于非活动状态也能正常工作的下载器。
我正在使用 NSURLSession
来处理这个请求。这是我初始化会话的方式:
@property (nonatomic) NSURLSession *downloadSession;
- (void)viewDidLoad {
[super viewDidLoad];
// Initialise the session
NSURLSessionConfiguration *sessionConfig = [NSURLSession backgroundSessionConfigurationWithIdentifier: @"backgroundSessionIdentifier"];
[sessionConfig setHTTPMaximumConnectionsPerHost:10];
_downloadSession = [NSURLSession sessionWithConfiguration: sessionConfig delegate: self delegateQueue: nil];
}
我还实现了下面的NSURLSessionDownloadDelegate
功能来查看每次下载的进度:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float totalProgress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
NSLog(@"URL: %@, progress: %f", downloadTask.originalRequest.URL.absoluteString, totalProgress);
}
我有一个 UICollectionView
,每个单元格都有一个按钮,点击后会创建一个下载任务:
// A delegate function of the custom UICollectionViewCell
- (void)beginDownloadAtIndex:(NSInteger)fileIndex {
NSString *fileURL = [self.urls objectAtIndex: fileIndex];
NSURLSessionDownloadTask *downloadTask = [self.downloadSession downloadTaskWithURL: [NSURL URLWithString: fileURL]];
[downloadTask resume];
}
现在说我在 UICollectionView
中有 10 个项目。我将 HTTPMaximumConnectionsPerHost
设置为 10,以便我可以同时下载它们。
发生的情况是我创建的前 3 个下载任务将同时开始下载,但其他 7 个不会。当前 3 个下载任务之一完成时,"dormant" 下载任务之一将开始下载。
我可能做错了什么,因为当我尝试使用默认会话配置时 [NSURLSessionConfiguration defaultSessionConfiguration]
,所有 10 个下载任务将同时开始下载。
如果有人能帮我找出我的代码有什么问题,我将不胜感激。
后台会话在单独的后台守护程序中发生在进程外。 Apple 似乎很可能故意限制更改该参数的能力,以防止在您的应用程序未运行时出现异常大量的传输运行。
您可以向 Apple 提交错误,但我怀疑他们会以 "Behaves correctly".
的形式发回
也就是说,我不确定您要通过使用大量并行下载来实现什么目的。除非 ISP 有异常高的数据包丢失或故意节流(例如突发模式),运行 十个大型下载一次三个和 运行 一次十个之间应该没有太大区别,在平均。
我正在尝试为我们的应用创建一个简单的下载器。该应用程序将从我们的服务器下载大文件(每个文件至少 100 MB),因此我想实现一个即使应用程序处于非活动状态也能正常工作的下载器。
我正在使用 NSURLSession
来处理这个请求。这是我初始化会话的方式:
@property (nonatomic) NSURLSession *downloadSession;
- (void)viewDidLoad {
[super viewDidLoad];
// Initialise the session
NSURLSessionConfiguration *sessionConfig = [NSURLSession backgroundSessionConfigurationWithIdentifier: @"backgroundSessionIdentifier"];
[sessionConfig setHTTPMaximumConnectionsPerHost:10];
_downloadSession = [NSURLSession sessionWithConfiguration: sessionConfig delegate: self delegateQueue: nil];
}
我还实现了下面的NSURLSessionDownloadDelegate
功能来查看每次下载的进度:
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
float totalProgress = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
NSLog(@"URL: %@, progress: %f", downloadTask.originalRequest.URL.absoluteString, totalProgress);
}
我有一个 UICollectionView
,每个单元格都有一个按钮,点击后会创建一个下载任务:
// A delegate function of the custom UICollectionViewCell
- (void)beginDownloadAtIndex:(NSInteger)fileIndex {
NSString *fileURL = [self.urls objectAtIndex: fileIndex];
NSURLSessionDownloadTask *downloadTask = [self.downloadSession downloadTaskWithURL: [NSURL URLWithString: fileURL]];
[downloadTask resume];
}
现在说我在 UICollectionView
中有 10 个项目。我将 HTTPMaximumConnectionsPerHost
设置为 10,以便我可以同时下载它们。
发生的情况是我创建的前 3 个下载任务将同时开始下载,但其他 7 个不会。当前 3 个下载任务之一完成时,"dormant" 下载任务之一将开始下载。
我可能做错了什么,因为当我尝试使用默认会话配置时 [NSURLSessionConfiguration defaultSessionConfiguration]
,所有 10 个下载任务将同时开始下载。
如果有人能帮我找出我的代码有什么问题,我将不胜感激。
后台会话在单独的后台守护程序中发生在进程外。 Apple 似乎很可能故意限制更改该参数的能力,以防止在您的应用程序未运行时出现异常大量的传输运行。
您可以向 Apple 提交错误,但我怀疑他们会以 "Behaves correctly".
的形式发回也就是说,我不确定您要通过使用大量并行下载来实现什么目的。除非 ISP 有异常高的数据包丢失或故意节流(例如突发模式),运行 十个大型下载一次三个和 运行 一次十个之间应该没有太大区别,在平均。