Beginning Networking with URLSession

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

Part 1: Introduction to URLSession & Concurrency

04. Challenge: Run Code on the Main Thread

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: 03. More Modern Concurrency Next episode: 05. Session Configurations

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: 04. Challenge: Run Code on the Main Thread

Concurrency - The Swift Programming Language

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

Welcome to the fourth episode. We haven't talked about URLSession yet, but don't worry. That is coming up in the next video. For now, time for a little challenge for you. Remember that when your app is downloading a video or a song, you don't want the user thinking your app has crashed because it's unresponsive. That's why you do stuff in the background. What happens when you finish doing a long running task? Can you just update your controls from the background? The answer is yes, followed by you probably don't wanna do that. The UI isn't concurrency safe or thread-safe, meaning it's not designed for multiple threads to access it at once. It'll corrupt the state of the user interface and probably cause your app to crash. You saw about that in the previous episode on how actors and the MainActor can help with this. All updates to your user interface must be performed on the main thread and sometimes you may want to perform other codes specifically on the main thread. That's precisely what this challenge will help cover. To get started, open this episode, Starter Playground. Take a look at the existing printTitle function. Your goal is to update this function in order to ensure both print statements are run on the main thread. Remember that you can use MainActor both as an attribute of task or by itself to run code on the main thread. Good luck! (upbeat music) Welcome back. How did it go? There are two ways to achieve your goal, each with some important differences. Update your printTitle function as follows. Before calling both print statements, you use MainActor.run with await preceding it in order to indicate that you want the contents of the closure to execute on the MainActor or main thread. Run your playground now and look at the results. Fantastic! As I mentioned, there's an alternative way to also run this code on the MainActor at the following function below printTitle. This time you use the MainActor attribute to indicate that you want this function to execute on the MainActor. Next, update your task at the bottom of the playground and run your playground one last time. You get the same expected result. The important thing to note about the difference between both approaches is that this last implementation using the MainActor attribute runs the entire functions code on the MainActor, which may or may not be what you want. If your goal is to run your loop in the background until you find the line that contains the title and then print out the results on the main thread, then the printTitle method is the correct implementation. Should you not care about having your loop run on the MainActor, find the line that contains the title and then print the results still on the main thread, then printTitleAnnotated is what you want to use. And there you have it. Congrats on making it all the way to the end of this challenge. In the next episode, we'll begin looking at URLSession and how to work with it. See you there!