NSURLSessionTaskPriority 似乎被忽略了?

NSURLSessionTaskPriority seems to be ignored?

在这个例子中,我以默认优先级启动 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.

您的服务器需要支持HTTP/2。
NSURLSessionTaskPriority 是 HTTP/2 优先级框架/依赖权重的包装器。

NSURLSession's support of HTTP/2