DispatchQueue

Contents

keyboard_tab
Threading, Async & Await
1
Threading Project
PREVIEW9m 21s
4
8
Views
9m 13s
9
10
11
Actors
7m 43s

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
2h
Students
13
Ratings
5/5
starstarstarstarstar
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. So, within this lecture we're going to make things right. And we're going to introduce some concurrency codes into our application because as you that see, we cannot interact with the user interface and it's not good, okay. We have to do something in the background in order to not to block the UI. So, how do we do that? Of course, we're going to have to change some things. We're going to have to introduce some new codes, and we're going to do it with the DispatchQueue. So, we have worked with the DispatchQueue and we have talked this a little bit but we haven't actually seen the details. So, DispatchQueue let me try to bring this up a little bit like make this thing bigger. Here we go. So, this is like a class that manages the execution of tasks. So, it's going to determine whether it's going to be a serially or concurrently running, right? So, we can try to run the things in the main thread or the background threads. So, main thread is the main thread, the user interface thread. So, what user sees should be on the main thread, okay? But like this long running task  like downloading something from the Internet or maybe if you try to maybe sort an array containing like a thousands of numbers inside of it or thousands of words, then you can do this on the background as well, okay? So, if something's going to take like, like a long time or something, it's something like a CPU intensive operation, then you're going to have to just do it on the background thread  in order not to block the main thread, in order not to block the UI thread. So, the UI thread is the main thread. So, everything that user sees or interacts, should be in the main thread as well. So, we have done this before like DispatchQueue.main.async. So, we have talked about this a little bit when we dealt with the URL session. So, URL session is actually doing everything concurrently or on the background on its own. But when we actually try to update the user interface like just give the data to the array or to the Table View now we have used this code block and it was good, it was okay, it was right thing to do, but in this case, I'm not actually doing this thing in the main. So, I want to do this thing on the background so, I'm going to call global.async, okay? So, it's going to be asynchronous again but it's going to be on the background thread. So, this is good but by the way, if you do it, of course, you're going to have to put self  on every parameter over here because we are right now in the closure. So, self.data = try! Data(contentsOf: URL  (string: self.urlStrings [self.tracker])!). Great, now it's going to right now download the thing in the background and that's good. But what happens if I try to just do this  self.imageView, self.data. So, this is related with the user interface, right? This is what user sees. So, in order to make things right, I need to do this in the main thread. So, for this line, is going to be background thread  and for this line, is going to be main thread. So, it should be on the main thread. But right now, I'm inside a closure and it's the global async, right? So, what do I do? Of course, I can do the same thing again, I can just convert this to be executed in a main thread asynchronously like this. So. DispatchQueue.main.async like this. Here you go. Now, it's going to download the data on the background and it's going to show us the data on the main thread  and it's exactly how it's supposed to be. Now, I'm going to copy this because we're going to do the exact same thing under change image as well. So, when we change the image, it will be downloaded on the background, and we will be able to interact with the user interface without any risks. So, that's great. And I believe that's it. So, right now we can try this. So, this is solution that we have been working on so far with you noticing or without you noticing it. And by the way, as you can see, we cannot see the ImageView right now, and I can scroll down and up. And the reason that we're not seeing the ImageView yet, I don't know. Maybe my Internet connection slows a little bit, but we will be able to sit  once it's downloaded there. Here we go. Now, we can see it. Now, let me try to change this. Here you go. I clicked on the button. Now, I can interact with the user interface. Now, I'm waiting for that very big image to be loading over here, and here you go. It's loaded right now. Of course, in this case, you can ask that what happens if a user taps on that button. And we're not showing anything, and it can take like a couple of seconds in order to refresh this image. What will happen then? So, let me just show you. I'm going to click on this button and I can interact with this, but it takes a couple of seconds to change the image right? So, it's not a good user experience as well. So, of course, we can add like a progress bar or a spinning bar over here in order to let the user know that we are doing something on the background and be patient. We're going to show you what you want. But it's actually a much better implementation rather than writing everything serially and risk the application to be crashed and not doing everything concurrently like we do right now, okay? So, this is what we have been talking about, this is how trading works. Right now we can do multiple things at once and we can decide whether to do this on the background or in the main thread. Of course, we can use the SD web image or any other pod like or an external library like we did before, but I wanted to show you the background logic, the main thread logic, and DispatchQueue logic. And that's not the end of this story at all because we will learn some new concepts. we're going to learn about something called async and await. So, these new ideas have been introduced in mid 2021, and they will be very popular starting from the 2022. And you will be able to actually use them in your applications and they will be a lot that you will be facing them a lot in interviews, in job interviews, in job seeking marathons or bootcamps  or something like that as well. So, we have to understand this first in order to understand them later on. And also we have to understand the MVVM structure, the swift UI in a better way in order to land a better jobs in IOS and Swift as well. So, what we're going to do, we're going to continue with the SwiftUI async image concept and then later on, we're going to write the crypto crazy that we have seen before in the SwiftUI with the new async and await features. And we're going to see how they can give some advantage to us when we build similar applications. Now, let's stop here and meet you in the next lecture.

 

About the Author
Students
899
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