Start course

This course explores the concepts of threading, async, and await in iOS and how you can employ them in your app builds.

Learning Objectives

  • Understand how to implement threading (aka concurrency) in your iOS apps
  • Learn about the concepts of async image and await and how to use them

Intended Audience

This course is intended for anyone who wants to learn how to develop apps on iOS.


To get the most out of this course, some basic knowledge of iOS development would be beneficial. We recommend that you take this course as part of the Developing Mobile Apps for iOS learning path.


Hi, within this lecture, we're going to talk about the actors and also we're going to see how to implement a sync functions in the buttons as well. So far, we have completed our applications so we have this web service and the download currencies continuation function over here and we're not even going to change that. What I want to talk about is the dispatch 'queue.main.a sync' codes. We do that in the view model because we know that the crypto list change will eventually affect the main view. When we change this crypto list, it will affect the main view and the main view will get re rendered or updated according to these changes because it's an observed object over there. But what if we want to get rid of 'DispatchQueue.mean.async'?

Because maybe we have multiple variables over here like '@Published var', '@Published work', all we got to do is just come over here above the class definition and add main actor. '@MainActor'. So, this code will enable us or enable this class to execute every crypto a list or anything similar published verse or any property inside of this class to be executed on the main track. So, whenever we change this Crypto list, it will be on the main track. So, I can just delete it. That's it.

So, you got to add the main actor and you got to just delete the dispatch queue and maybe we don't delete it. Maybe we just comment this out so you can stay on your notes, so you can remember why and when we use it but we can just copy and paste this like this is. And here we go. It doesn't matter if we have multiple published verse right now. Of course, we can have it. As long as we add this '@MainActor', they all will be executed on the main threat. So, this is cool. Maybe you don't want to add this main actor. Maybe you want to just do it manually and that's fine too. But no, you can use this and we don't even have to change anything else. Just run this and here you go, it works fine. As you can see, we can use this main actor thing in order to execute these variables on the main threat  and it's a nice way to deal with this main threat, background threat thing that we face. So again, you can have as many as published var over here in your view model and that's fine. You can either add this main actor and make them all executable in the main threat or you can just do it manually by placing 'DispatchQueue.main.async' over here. So, that's great. But I want to show you something else. Right now, we are inside of a task and we can call this await function or async function with an await syntax and it's doable because task is by definition it's an async scope. And we couldn't do that under 'onAppear'. So, what happens if we have a button, for example, a refresh button on our application where we want to click it and where we want to execute this exact same code. Let's try to add a toolbar to our application to see what happens then. So, I'm inside of this and above the navigation title, the list as a list property I'm adding '.toolbar'. So, this is a regular toolbar. We can have a button over here, we can have a task or text or whatever we want. So, all you got to do is just choose this toolbar content and hit "Enter". It will ask you to just put anything you want over there, like a text or a button or maybe an icon or something like that. But I'm just going to add a bottom and choose the action and label.

If you hit "Enter", it will give you a coding block for the action itself. So, whenever you want to write over here will happen when button is clicked. And for the label, I'm just going to display a refresh text. So, when you use your taps on this refresh, we're going to refresh the data. So, let me run this and see if the toolbar works. And here you go. Toolbar works and we can see the refresh button over here. So, you'll see that thing in applications all the time. So, since my function is async right now, what happens if I copy and paste this, what happens? So, I'm pasting it and I'm just writing a wait but it gives me an error because this button is not in the scope of the async, okay, it's not an async scope, so I cannot run that. So, I have previously solved this with that task but I cannot do that over here. So, what should I do? So, the previous way to do this was this; async and await. So, async can be executed as a coding block as well as you can see  but now, they deprecated it. So, rather than async, they actually say that you have to use task like this, but it doesn't work at this point as well. Right now, we have to say, 'Task.init'. So, I'm showing you all these alternatives because swift UI changes every time because it's a new concept. And as you can see, once I hit that, once I just add this async, it gives me the warning message to use this 'Task.init',  and here you go, we can use it. So, if I run this right now, what happens? When I hit on the refresh button, it should change, it should actually download the data from scratch. But since our data is static, I cannot get the change data, I cannot show it to you. But in order to make sure, I'm going to do something like this. I'm going to change the crypto list, View models, crypto list to an empty list before I download it. Of course in a real application, you shouldn't do that. I'm just showing you so that you would understand that this task scope is working. Right now, I'm going to hit the "Refresh" and as you can see, for a second, the list goes blank and then it goes back because it downloads the data to display it on the screen again. But in a real application, you shouldn't use it. So, I'm just going to comment this out. But this is how you actually use the await async, await functions, async functions inside of a button. Great. Now, we have seen a lot regarding the concurrency, regarding async and await and also, the good features of swift UI and combined framework like observable objects, observed objects published. These subjects, these concepts, will be heavily used in Swift in the upcoming years. So, it's better for you to learn them right now and try to integrate them in your projects as much as possible. So, thank you for watching this section, see you in the next one.


About the Author
Learning Paths

Atil is an instructor at Bogazici University, where he graduated back in 2010. He is also co-founder of Academy Club, which provides training, and Pera Games, which operates in the mobile gaming industry.

Covered Topics