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.
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.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.