NSURLSessionTaskPriority seems to be ignored?
Asked Answered
R

2

6

In this example I kick off 100 NSURLSessionDataTask at default priority but then set the last one to high priority. However it seems to have no effect at all as the last task still ends up running last.

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];
}

Why doesn't this task with high priority run before the other 99 at default priority? I would expect maybe a few of the earlier tasks might start to execute before the high priority task was even added, but not all of them.

I have even tried with HTTPMaximumConnectionsPerHost set to 1, makes no difference.

Reverse answered 1/6, 2016 at 6:56 Comment(1)
Is it an upload or a download task?Mensurable
M
8

The priority does not influence the priority on your app's side. It is a hint for the host to response to several requests in a priority. The host can completely ignore that.

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.

Mensurable answered 18/6, 2016 at 19:16 Comment(8)
So it looks like I completely misunderstood this. Priority has no effect on the queue order at all, a high priority task added last is going to be started last. It's completely useless lolReverse
No, it isnot useless, because it is transmitted to the host. However, the host, not the client decides when data for a download is sent.Mensurable
But it's not transmitted to the host until the 100 low priority tasks have already been completed. Completely useless. So if I want something done with priority I need to use a seperate NSURLSession.Reverse
Again: This setting is for the host. It is meaningless, if the client never reaches it, because he decides to have another priority for scheduling the requests. It is not meaningless, but meaningless for what you want to do.Mensurable
I understand this setting is for the host. I just can't see any scenario where it has an actual use - hence useless.Reverse
You have multiple requests in parallel and want to tell the host, which request he should serve with a higher priority. Please keep in mind, that when the server sends much data, it is he decision which data to send first. In such case the client will receive the high priority data first.Mensurable
@AminNegm-Awad I have a question about this. I'm sniffing my requests with different NSURLSessionTaskPriority values. But I don't see any difference between them. (at least in HTTP layer) Do you know how this priority is transmitted to the host?Abessive
Without deeper knowledge I remember, that TCP/IP had precedence or QoS bits … But maybe this is not correct. For sure it depends on the protocol.Mensurable
G
0

Your server need to support HTTP/2.
NSURLSessionTaskPriority is a wrapper to HTTP/2 priority frames / dependency weighting.

NSURLSession's support of HTTP/2

Geosphere answered 17/3, 2020 at 3:38 Comment(3)
This may be true but it's not the answer to my issue. See the other answer above.Reverse
@Reverse Actually, my answer explains why the hint not working.Geosphere
No sorry it doesn't, it was a client issue. The high priority task wasn't even attempted to start until the low priority tasks were all complete.Reverse

© 2022 - 2024 — McMap. All rights reserved.