Previous episode: 08. Introduction
Next episode: 10. Concurrency With async let
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.
For this part of the course, you'll work on the cloud file storage app. Your first job is to download this list of files from the server, and display the list in this view. It's very similar to how you downloaded and displayed the list of stock symbols for Little John. So you complete this job as a challenge to reinforce what you learned in part one. See how much you can do without peaking at Little John. Most of the projects in this course interact with the server, which is included in the course materials. If you skipped part one of this course, start the server by following the instructions at the beginning of episode three. If you're continuing from part one of this course, refresh your browser to make sure the course server is running or restart the server in terminal. If you leave the server running, it might stop serving data correctly. If the app doesn't work as you expect, stop the course server, and then restart it. In the course materials, locate the super storage starter project and open it. (dramatic music) Your challenge is to download the list of files from the server and then display the list in this view. Finish writing the available files method in model SuperStorageModel. You'll decode the JSON data as an array of DownloadFile values. And add a test queue modifier to ListView to call availableFiles and store its returned array in files. Good luck. (upbeat music) Welcome back. Hopefully you had success with this task. Here's how I did it. In SuperStorageModel, here's availableFiles. This looks very similar to availableSymbols in LittleJohnModel. The endpoint is different, and it returns an array of DownloadFiles. The method's signature includes async throws to tell the compiler and the Swift runtime how you plan to use it. Async says this method is asynchronous. The runtime can suspend its execution until it returns a result. The compiler makes sure you don't call this method from synchronous contexts that don't allow the method to suspend and resume the task. Remember, in Little John, you had to call availableSymbols from a task view modifier because SwiftUI views are synchronous. Now, to fetch the data, I added this line before the return statement. This is exactly the same as the data from call in Little John. Calling the URLSession async method with the await keyword tells the runtime it can suspend this method's code until data from returns a response. And the system can use the thread to do other work. Next, to check the server responds and return the fetch data, I replace the dummy return statement. Just for a change, I'll throw my own error if decode fails. So this method either returns an array or it throws an error. Next, I updated the SwiftUI view to use this new async method. In ListView, I added code where the todo is, just below the dot alert. First, I check if I already fetched the file list. If not, I call availableFiles. And I catch and store any errors in lastErrorMessage. The app will then display the error message in another box. This is exactly like the code in Little John's SymbolListView but with files instead of symbols. As you learned in Little John, task is a view modifier that allows you to execute asynchronous code when the view appears. It also handles canceling the asynchronous execution when the view disappears. To test this, stop the course server, and then build and run. No surprise, I get the error could not connect to the server. The URLSession asynchronous method threw this error and it propagated up the task hierarchy to the task in this view. Throwing errors is a key advantage of Swift concurrency. It provides a consistent robust way to handle errors. I restart the course server, and then build and run again. And there's the list of files. The files are TIFF and JPEG images. To save space on your Mac, they're all the same image. The server creates TIFF and JPEG representations of a gradient image. That takes care of the ListView. Next up, you learn how to download the server status at the same time as the list.
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.