Beginning Networking with URLSession

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

Part 1: Introduction to URLSession & Concurrency

07. Challenge: Fetch Data Over the Network

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: 06. Get Data from a Session Next episode: 08. 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.

Learn more Already a subscriber? Sign in.

Notes: 07. Challenge: Fetch Data Over the Network

URLSessionConfiguration - Apple Developer URLSession - Apple Developer

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

Welcome to the seventh episode. Time for a little challenge for you. In this challenge, you'll take a basic Swift playground that contains some model code and SwiftUI view code in order to download data from the iTunes search API, parse it, or decode it as it's often referred to, and even display part of the data in your user interface. To get started open this episode's starter playground. The main playground page just sets up the playground to show your SwiftUI view. Switch over to MediaView.Swift and look at the contents. This view sets up a button that makes the network request to search the iTunes API and shows the results in a list. Towards the bottom of the file, there is an async method with some boiler plate code for you to get started with your data download. Note also that this method is called inside the buttons action using a task, since the method awaits as it's marked async. Moving on, open MediaResponse.Swift. This file contains a couple of structs modeled after the iTunes search API response. Your goal is to make the iTunes search API request parse the results into the model objects and show them on your lists. Remember that you can reference and look at the previous episodes' code and use JSON Decoder in order to parse the response into your MediaResponse object as opposed to a string, like you did in the previous episode. Good luck. (funky music) Welcome back. How did it go? Let's go over how you could have solved the challenge. Open MediaView.Swift and add this code where the to do was. Next, create a task where the URL sessions method will execute. Inside the task, make the actual call to download the URL's contents. Similar to the previous episode, validate your response or throw an error otherwise. Next, in referencing the commented out code at the bottom of the method, which you may have deleted, add the following at the bottom of your task. This code uses JSON Decoder to parse the response into an objective type MediaResponse. Just as when validating the response, if decoding fails then you throw an error. Finally, update your music items property in order to update the user interface and list out the response items. Run your code. As we discussed in earlier episodes, all UI updates must be performed on the main thread. Because music items is a state property that's managed by SwiftUI and thus will trigger a UI update, setting its value must be done on the main thread in order to avoid potential issues. And that's one of the things to keep in mind about concurrency. You may have forgotten to do this on the main thread and yet nothing crashed or went wrong, but that doesn't mean that the code isn't without problems. Great work. That was definitely a challenging challenge. You used a lot of different concepts and knowledge to get to your goal, so kudos to you for making it all the way to the end.