Previous episode: 15. Using Combine
Next episode: 17. Conclusion
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.
Make sure the course server is running and continue with your project from the previous episode or open the starter project for this episode. In this episode, you'll implement the premium download plan Cloud 9. It downloads files super fast by running four concurrent tasks. Each task downloads about one fourth of the file. You'll start by finishing the private version of downloadWithProgress in SuperStorage model. Open SuperStorage model, and locate the if let offset code you commented out in episode 13. Offset is nil for the silver and gold plans, so you didn't need this if closure. For the Cloud 9 plan, you'll pass in an actual offset value to create four tasks. To start, uncomment these lines. In the closure, create a urlRequest. You probably haven't seen this Initializer before. And then, click URLRequest and select Jump to Definition. Select the second option to open Utility.swift. This initializer is a custom extension of URLRequest. The core server you're running supports capability called partial requests. You can ask for a byte range of the response instead of the entire response at once. You do this by setting the Range field in the HTTPHeader. Here's an example of offset length and range values for 77,345 byte file broken into 20,000 byte chunks, three 20,000 byte chunks, and the remaining bytes in the last chunk. When offset is 40,000 and length is 20,000, the range value is bytes = 40,000-59,999. Don't worry, the starter project code already handles all the calculations to create these chunks. Now click back to SuperStorage model to send the request and store its result. In episode five, you used the from: url version of bytes with a custom live URLSession to get a never ending sequence of stock prices. Here, you use the for: urlRequest version of bytes. You don't need this session to stay open, so shared is fine. This got rid of the error message about uninitialized result. To finish this closure, check the statusCode almost as usual. statusCode 206 indicates a successful partial response. Now, jump down to multiDownloadWithProgress. This method already includes the code to break a file download into four parts. This partInfo helper function calculates the offset size and name values for each part and stores them in the parts array. Jump back to look at the private downloadWithProgressSignature. Name, size, and offset are precisely the parameters you need to pass to this method. Jump back to multiDownloadWithProgress, and start replacing this dummy return statement with calls to downloadWithProgress. Start with part Zero. Define a promise with async let, then duplicate and edit this line. Next, await an array of these four downloads. This executes them concurrently. Now, combine their data. The default data initializer gives you the initial value for summing over the array items. And now, return the complete file content instead of the dummy placeholder. Finally, you just need to call multiDownloadWithProgress in DownloadView. Find the code for downloadSingleAction and copy/paste/edit it into downloadMultipleAction. You don't need to store the task in downloadTask. The course server doesn't slow down partial responses, so files download faster than you can move the cursor to cancel now. Build and run. Select the tiff file and tap Cloud 9. How awesome is that?
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.