Beginning Networking with URLSession

Sep 13 2022 · Swift 5.6, iOS 15, Xcode 13.4.1

Part 2: Download Data

10. Priorities & Cache Policies

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 09. Introduction Next episode: 11. Download Music

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Notes: 10. Priorities & Cache Policies

URLSession - Apple Developer

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Before you go any further, there are two very important concepts to understand, priorities and cache policies. Let's start with priorities first. When you create a task, such as a downloading task, you can suggest the priority level for that task to run at. This priority suggests how to organize the session tasks from your app. The keyword being suggests, as at the end of the day, the operating system will determine the ultimate priority for your task, but a little help goes a long way. For instance, you may be downloading some data in the background, but the data isn't that important, maybe you're downloading the latest version of your documentation. By default, the system will assume your task is a default priority, but you can set it to a low priority instead. The OS may respond to your suggestion to run on a low priority, or if the system isn't particularly busy, it may keep your task running at a default priority. Later, it may switch it to a lower priority when more resources are taken. This priority is a number that ranges from zero to one. The default priority of a task starts at 0.5, that's the midpoint. URLSession does provide name priorities, such as default priority, which is 0.5, low priority for 0.0, and high priority for 1.0. You set the priority on the priority property of any task object. Once you set the priority level, you aren't locked to it, you can switch a tasks priority level at any time. Along with sessions, you also have cacheing. Larger downloads can make good use of cache to reduce network traffic. This flow chart is from Apple's documentation. It's pretty much the way you'd expect the cache to be used. If a cached response does not exist for the request, the URL loading system fetches the data from the originating source. You have lots of different cacheing options, from ignoring all caches and making a fresh request every single time, to using a cache regardless of age of expiration time. You have lots of options. The default cache policy is to use the protocol's cache policy. The protocol is usually HTTP. You set your cache policy on a URL request, where you can set the cache policy on the session configuration object using the property request cache policy. If any of those policies don't fit your use case, you can manage the cacheing yourself. You implement the delegate method will cache response. This delegate method is only for uploads and data tasks. It is also not called for background or FML configurations. For more information on caching and how to access the cache data, see Apple's accessing cache data article on the developer portal. Now it's time to put some of these concepts into practice. So I'll see you in the next episode.