Search code examples
objective-cnsurlsessionnsurlrequestnsurlsessiontask

NSURLSessionTaskPriority seems to be ignored?


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.


Solution

  • 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.