IOS network call prioritization
I want to use good prioritization of network calls in iOS.
So far I am using URLSessionTask.priority
. I create network calls like this:
URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data, let response = response {
// Handle response
} else {
// Handle error
}
}
task.priority = priority
The problem is that I can still see that priority 0.0 load tasks take a significant load (up to ~ 60%) from priority 1.0 tasks as soon as they start. Sometimes, priority 1.0 calls are only triggered when more low priority requests have completed. Apple ( documentation ) confirms that:
To specify the host for prioritizing session URL tasks from your application, specify a priority for each task. Prioritization is only a hint and does not guarantee performance. [...] The API does not allow you to determine the effective priority for a task from the point of view of hosts.
I want to implement presumptive loading and caching without degrading the performance of user-initiated requests. This is especially important for devices with poor connectivity.
The solutions I have considered:
- Using Alamofire (but afaik they also have no priority)
- Create a custom solution that will allow concurrent downloads
n
, have a priority queue, and interrupt / suspend very low priority requests when high priority requests come in. To me this sounds like something that should already exist, and it also sounds a bit like a fantastic way to shoot yourself in the leg if it goes wrong.
The alternatives I have considered:
- Launch less important calls after making the most important calls
- Do not execute queries at all on bad connections (question about how to detect a bad connection),
I support iOS 9 and newer, but will also look at solutions that don't work on iOS 9.
source to share
No one has answered this question yet
Check out similar questions: