Previous episode: 14. Show Download Progress
Next episode: 16. Simulate Different Network Speeds
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.
Welcome back. At times you may wanna group requests together. Right now you download the song and album artwork is two separate requests. Each from two different methods. What if you wanted for song detailed view to make a single method call and get back both at once. How'd that work? Let's take a look. Open to starter project for this episode and then go to Song Downloader dot swift. Add a new method towards the bottom. This method receives two URLs as parameters and returns a data object. It's asynchronous, so it's marked a synch and can throw error, so throw is also in the method declaration. Next, add this code. You declare a type alias for a tuple containing a URL object and a URL response object. Moving onto the actual requests, add the following. Oh my, what is this? You're declaring a synchronous constants that will execute immediately. The results are stored in the constants themselves and can be awaited afterwards in order to proceed once all requests have completed. This is exactly what you want in order to group your requests. These two requests don't need to be sequential and execute in a specific order. You just want to take advantage of the network and system resources to have them execute as fast as possible, regardless of the order in which they complete, and then proceed. To actually wait for both of these requests to complete, you need to add the following. This awaits for both requests to complete, marks them with try in case any errors are thrown that need to be handled, and stores the results in a couple of constants. You could also await an array of a synch properties and store those results in a single constant array. That could work if you want to download, say many items at once. For this scenario, it's more convenient for you to have constants for each of the result tuples. One for the song, and one for the artwork. Add this code next in order to check the responses and their status codes. This should look familiar as it's the same code you've used a couple of times when performing requests. Moving on to the file manager side of things add the following. Again, this is exactly the same code as written in the other download methods except combined to handle both the song and artwork downloads. Continuing, add this code next. With both items downloaded time to wrap up this method. Add the following code at the bottom of the method. This will set the download location property on the main thread. And then add this. This attempts to initialize a data object with the images contents. At this point you've done everything that's necessary in order to handle both the song download and the artwork download. This method is essentially a combination of the two separate methods. You leverage file manager to store the downloads in a permanent location, set the download location property for the song, and return a data object with the artwork. Build your project just to make sure everything is running correctly. You'll take care of updating the UI next, but before you do it's important to know that all three of the download methods have a lot of duplicate code in them. You're not going to worry about that for now as repetition is great when learning and trying to memorize new concepts. But should you be inclined to tidy things up a bit and remove some of the duplication feel free to add helper methods to put some of the duplicate logic into a single method or refactor this class as you see fit. Time to work on the UI side of things next. Open song detail view dot swift. Comment out the on appear modifier. You use this to immediately download the album artwork when the view was shown, but now you want to use your new method that groups the song and artwork requests. Too keep things separate add a new method. Then add the following code inside. First, you check whether the song has already been downloaded or not, as you don't want to download it twice. And that's the main item you want to download from the internet. Next up you ensure you can construct URLs for the song and artwork. Otherwise you simply return from this method. You could add some error handling here in order to notify your users, but for now we skip that in order to focus on the concepts at hand. Finally, should this song already be downloaded? You set play music to true. After the guard statement add this code next. This sets is downloading to true as you'll begin to download next. And then it sets it back to false, but within a diverse statement, So this gets done before the method returns. Time to use Song Downloader to actually download our resources. The new method returns the data object for the artwork image and sets Song Down loader's download location property for the song. Knowing that you store the return data in a constant and try to create an image from it. If you succeed, then you set the view's artwork to your new image. Nothing special needs to be done for the song itself as that's taken care of via the download location property of Song Downloader. Should the download method fail, you catch the error, and for now print it and set the property that shows your error alert. Finally, and to wrap it all up, update the button action with the following. This simply replaces the method that gets called to perform the downloads. Voila, all done. Build and run the app and check out the results. Fantastic! Your requests are now grouped. Note that you're not worrying about the download progress or fancy error handling here. You've seen how to do that already so the focus is on showing you how to group requests. In the next episode, I'll show you how to simulate different network speeds and conditions. That's going to come in handy for properly testing the progress view you implemented in the previous episode. See you in a bit.
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.