8

I want make use of good prioritisation of networking calls on iOS.

So far, I am using URLSessionTask.priority. I create networking 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 still see that download tasks with priority 0.0 take significant load (up to ~60%) from tasks with priority 1.0, as soon as they start. Sometimes, calls with a priority 1.0 only start when a larger number of low-priority requests completed. Apple's (documentation) confirms 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. [...] There is no API to let you determine the effective priority for a task from a host’s perspective.

I want to implement presumptive downloading and caching, without reducing the performance of user initiated requests. This is especially important on devices with poor connectivity.

Solutions I have considered:

  • Using Alamofire (but afaik they don’t have prioritisation either)
  • Create a custom solution that would only allow n parallel downloads, have a priority queue, and abort/pause very low priority requests when high priority requests come in. To me, though, this sounds like something that must exist already, and also a bit like a fantastic way to shoot myself in the foot, if implemented wrongly.

Alternatives I considered:

  • Only start less important calls after the most important calls are done
  • Don’t do the requests at all on bad connections (follow up question would be how to identify a bad connection)

I am supporting iOS 9 and newer, but would also consider solutions that don't work on iOS 9.

Sebastian Hojas
  • 4,158
  • 2
  • 27
  • 38
  • What do u mean by "tasks with priority 0.0 completely block tasks with priority 1.0 " Are u running tasks on a serialized queue ? By default NSURLSession allows u to run multiple data tasks at a time. So even if low priority tasks start they should run in parallel and not block high priority ones. You can always set HTTPMaximumConnectionsPerHost to fix the max number of concurrent connection u can open to host – Sandeep Bhandari Jul 12 '17 at 07:49
  • I would be good to see all the code. there might me something with the calls you are making and setting the priority – hariszaman Jul 12 '17 at 07:54
  • @SandeepBhandari Fair point, I clarified the question a bit. From my point of view, in an ideal solution, a high priority request would get the majority of networking capacity available, even when there are several low priority requests progressing. I have not yet set `httpMaximumConnectionsPerHost` because I was afraid that it would reduce performance on good connectivity, and it only works if there are a lot of connections to a single host. – Sebastian Hojas Jul 12 '17 at 08:11
  • @hariszaman I doubt that this is a code-related problem, but I'll try to create a sample project to illustrate the problem. – Sebastian Hojas Jul 12 '17 at 08:13
  • 1
    @sebastian-hojas : I agree setting httpMaximumConnectionsPerHost might affect the performance in good connectivity scenarios. As far as task priority is concerned Doc clearly says "Specifying a priority provides only a hint and does not guarantee performance" refer https://developer.apple.com/documentation/foundation/urlsessiontask/1410569-priority. That being said, If you have to achieve this capability of selective download, I afraid U'll end up creating a custom mechanism of ur own as u happened to mention in ur question :o I'll keep my eye on this question quite interesting one :) hence +1 – Sandeep Bhandari Jul 12 '17 at 08:17
  • 2
    I just stumbled over this... and I just can't believe that there isn't a "ready to use" solution for that problem. Neither Apple nor the Community (GitHub, Pods, etc.) seem to have a solution for that -.- So, is it too hard (or maybe too easy 0_o) to build a lib/framework for that? Or does just no-one really need this? (I would need it^^) – d4Rk Oct 01 '19 at 10:58
  • 1
    in 2021 did anyone find solution ?, I see similar UX in youtube app where in bad connectivity my video plays perfect but failed to load comments of that video :). – Jageen Oct 31 '21 at 04:31

0 Answers0