在这个例子中,我以默认优先级启动了 100 个 NSURLSessionDataTask
,然后将最后一个设置为高优先级。然而,它似乎根本没有任何效果,因为最后一个任务仍然最后运行。
NSURLSession *session = ...
NSURLRequest *request = ...
for (int i = 1; i<=100; i++) {
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"%d", i);
}];
if (i == 100) {
dataTask.priority = NSURLSessionTaskPriorityHigh;
}
[dataTask resume];
}
为什么这个具有高优先级的任务没有在默认优先级的其他 99 个任务之前运行?我希望在添加高优先级任务之前可能会开始执行一些较早的任务,但不是全部。
我什至尝试过将 HTTPMaximumConnectionsPerHost 设置为 1,没有任何区别。
最佳答案
优先级不会影响您应用端的优先级。 提示 主机按优先级响应多个请求。宿主完全可以无视。
To provide hints to a host on how to prioritize URL session tasks from your app, specify a priority for each task. Specifying a priority provides only a hint and does not guarantee performance. […]
[…]
You can specify or change a task’s priority at any time, but not all networking protocols respond to changes after a task has started. There is no API to let you determine the effective priority for a task from a host’s perspective.
关于objective-c - NSURLSessionTaskPriority 似乎被忽略了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37561483/