image
Threading, Async & Await in iOS
Threading Project
Difficulty
Intermediate
Duration
2h
Students
26
Ratings
5/5
Description

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.

Prerequisites

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.

Transcript

Hi. Within this section, we're going to discuss Threading or concurrency in iOS and in Swift in detail. So, concurrency or threading is in the simplest terms, the capability to do multiple things at once. So, we're going to do something simultaneously and actually we're doing it all the time in every kind of operating system. Right now in my macOS, I'm recording my screen, I'm recording my voice. I also opened the Xcode, and it can do everything at once. If it hadn't that capability, then I would be in trouble, right? So, iOS also has that capability as well, and we have been using it so far. So, what we're going to do? We're going to create a new project. And by the way, in order to make this, in order to get through this section, you're going to need the Xcode13 and above. So, if you have Xcode12 for some reason, then you're going to have to update but if you have like Xcode13, 14, 15, then it's okay. So, for the first app, we're going to just create an exemplary app where we discuss the theory of the concurrency. So, it will be very basic. All you got to do is just create a new app. We're going to use Storyboard in this application, in this particular application, but later on, we're going to move on to SwiftUI in order to learn some new things. So, I'm going to call this, ThreadingProject. So, make sure you choose the Storyboard from the interface. Again, we're going to go back to the SwiftUI later on, and I'm going to just put it on my Desktop and create this project. As I said before, this will not be a massive project. It will be just an exemplary project. We won't have datas or like something fancy, but we're going to understand how to work with threading, how to apply concurrency concept in our applications. Later on, we're going to actually build some real applications and just learn about the new features of the Swift and iOS as well, later on. Okay, so far so good. So far, we have been using the concurrency even if you knew what is the concept or if you knew it before or if you didn't. But right now, you're going to understand what we have been doing so far with the DispatchQueue and everything, and we're going to talk about it. And we're going to learn the async and await syntax that has been introduced in mid 2021 later on. So, let's go to Storyboard and create an exemplary UI over here. So, what I'm going to do, I'm going to add a Table View, but this Table View will not be expanding to all the screen edges rather, I will just make it a little bit smaller, like this because I'm going to add an Image View on the top of it and you're going to see why. So, let me take care of the constraints over here. So, what I want to have like zero constraints to the left and zero to right. And, it will be centered like this. So, maybe we can have zero constraint to bottom as well. Okay? But I didn't give the top constraint yet, so I'm adding these three constraints. Why I'm doing this? Because I want to add an Image View on the top of this Table View. So, let me just bring in an Image View from this, like that. Here you go. Now, I'm going to make this a little bit bigger and put it on the screen, like this, at the center. Of course, we're going to have to take care of the constraints for this Image View as well. And, this doesn't even make sense to have that kind of user interface in a regular application, but what I want to do is to download some image from the internet. And I'm going to show you the effects of it if we do something wrong. So far, we already have downloaded images from the URLs or from the internet in the firebase section and the other sections as well. So, we know how to do that but we don't know how to do it wrong. First, we need to know how to do it wrong so that we can see the effects of doing wrong, and then we can see how to do it right, and understand it in a better way. So, that's why I'm just bringing this UIImageView in. So, select the View Controller itself and embed in a Navigation Controller so that we can have a navigation title over here, because later on, I plan to add like a navigation bar button item. So, I'm going to open the constraints of this Image View and just give it something like 30 pixels over here. And yeah, I believe that's fine. Just give some constraints to left and right as well and maybe for the top as well. Just add some constraints, and of course, it will give some errors. So, I'm going to give the width and height constraints here as well. So, these numbers are not very important. We're just doing this for an example. All that matters is that you have like an Image View, and on the below you have a Table View, and it looks fine, okay? If you have this, then great. Now, I'm going to open the View Controller over here in order to connect the views that we have created inside of the View Controller as well. So, I'm going to control drag this, and call this, tableView. Of course, we're going to have to do the same thing for the Image View as well. So, control drag this, and label it imageView. Great. Now, let's go to View Controller and start writing some code. So, what do we have here? We have the TableView, right? And, I'm going to just do the setup of the TableView. We have done this so many times. At this point, it can be, it should be a second nature to you. So, I'm going to add the UITableViewDelegate and UITableVewDataSource over there. And, under your viewDidLoad(), I'm going to make the tableView.delegate = self and do the same thing for the dataSource as well. So remember, we have to overwrite two functions, first of which is the cellForRowAt indexPath, and also numberOfRowsInSection. This time.. Yap, here you go. This time, I'm just going to go over here and just return some constant number like 25. I won't even bother with creating an array for this. And for this, I'm just going to create a regular cell, like that, UITableViewCell. Here you go. So, let me just select this. And for the cell, I'm going to just use a textLabel. It doesn't actually want us to use the textLabel anymore, but I'm just going to keep things simple over here. All I want to have is like, "Threading Test" written 25 times in my TableView. So, that's why I did that. So, let me run this. And, what is wrong over here? It should be optional. So, put a question mark at the end of the textLabel. Great. So, what I'm going to do, I'm going to open this project in the simulator so that we can see all the text lining up, like 25 times in the TableView, so that we will be good to go. And all we are doing over here is that we're just creating this in order to test that if we do something wrong with the concurrency, what will happen? So, I have something going on inside of my simulator for some reason. I'm going to just do it like that so that I can make this a little bit smaller and just rotate this a couple of times more. Yeah, here you go. I believe now it's working. So, I'm going to make this a little bit bigger. It's kind of buggy for me right now, but as you can see, I have the Image View and I have the Table View. So, it moves, it scrolls down smoothly. Everything seems to be working. Great. So, the reason why I did that is, I have like a Table View over here and I can smoothly scroll down and scroll up. So, I'm going to download an image from the internet to show it in the Image View, and then we're going to see if we can scroll up and scroll down, if we do something wrong. We haven't come across that kind of problem before because we did everything right, but you have to know what happens when we do something wrong with the threading, with the concurrency. So, that's exactly what we're going to do within the next lecture. So, I'm going to stop here, and let's see you in the next lecture together.

 

About the Author
Students
2092
Courses
55
Learning Paths
3

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