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
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.
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.
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;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With