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.
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!
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.