Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession priority

I'm trying to port my old code for loading files from NSOperationQueue to NSURLSession. Almost everything is alright but I can't find how to set priority for loading different tasks. Does anybody know if NSURLSession supports prioritising at all? And if yes could you show me how, please?

Thanks in advance!

Rost

like image 904
Rostyslav Druzhchenko Avatar asked May 12 '14 06:05

Rostyslav Druzhchenko


People also ask

What is NSURLSession?

The NSURLSession class and related classes provide an API for downloading data from and uploading data to endpoints indicated by URLs. Your app can also use this API to perform background downloads when your app isn't running or, in iOS, while your app is suspended.

What is URLSession shared in Swift?

Discussion. For basic requests, the URLSession class provides a shared singleton session object that gives you a reasonable default behavior for creating tasks. Use the shared session to fetch the contents of a URL to memory with just a few lines of code.


2 Answers

There is a property on NSURLSessionTask

@property float priority NS_AVAILABLE(10_10, 8_0);

Its description says

  • Sets a scaling factor for the priority of the task. The scaling factor is a value between 0.0 and 1.0 (inclusive), where 0.0 is considered the lowest priority and 1.0 is considered the highest.
  • The priority is a hint and not a hard requirement of task performance. The priority of a task may be changed using this API at any time, but not all protocols support this; in these cases, the last priority that took effect will be used. If no priority is specified, the task will operate with the default priority
  • as defined by the constant NSURLSessionTaskPriorityDefault. Two additional priority levels are provided: NSURLSessionTaskPriorityLow and NSURLSessionTaskPriorityHigh, but use is not restricted to these.

I use it between a downloadTask and dataTask that could trigger simultaneously in our app and the dataTask waits for the download forever. But this fixes the issue

dataTask.priority = NSURLSessionTaskPriorityHigh;
like image 166
Irfanlone Avatar answered Sep 19 '22 13:09

Irfanlone


I'm not sure if this is available yet in iOS 8, but I found these lines in NSURLSession.h file

FOUNDATION_EXPORT const float NSURLSessionTaskPriorityDefault NS_AVAILABLE(10_10, 8_0);
FOUNDATION_EXPORT const float NSURLSessionTaskPriorityLow NS_AVAILABLE(10_10, 8_0);
FOUNDATION_EXPORT const float NSURLSessionTaskPriorityHigh NS_AVAILABLE(10_10, 8_0);

The apple iOS 8.1 release note is suggesting that these will be available in 8.1 though.

like image 43
Pahnev Avatar answered Sep 21 '22 13:09

Pahnev