Hi, Alex here from rebeloper.com and welcome back to the channel. Now in this
video I am going to talk about grand center dispatch or dispatch queues or
threading or whatever you would like to call them so without further ado let's
jump right into it.
Okay let me just show you what we are going to build out and
in the meantime you will learn about Grand Central Dispatch and threading on an
iOS app. So let's just build this test so I'm you may see what we are going to
create and talk about. So there is a label waiting for result and fetch data
from server, okay. We are going to push on the button and we are going to wait for
three seconds and the result is one the label has been changed to the result
what we have fetched from the server. Now of course this is a dummy fetching
because we are not having any server back to our app but we are
mimicking the way that iOS uses fetching meaning that we are going to fetch this
data on a background thread. Now what are background threads and main threads?
Well basically on the main thread the main thread is used to manage all of the
UI what you see is on the main thread that is for the utmost importance.
Now everything from data fetching or data processing is handled on a
background thread. Now for instance for if you are using firebase everything is
fetched on the background thread and then you need to let's say refresh your table
view to populate the data and that refresh needs to be done on the main
thread. So basically on the main thread you handle
the UI on the background thread you handle all of the fetching and
processing and so on. So we are going to simulate this fetch data from server on
a background thread. Now enough talking let's just go ahead, let's just stop this
and go ahead and click the link down below and you will see the resources and
you will get the link to a my github repository and it is called easy GCD
Grant Center Dispatch and go ahead and clone it or download it and you will
see something like this. Once you let say you download it and without this pod
file log and the pot you will have a pod file. Now what you want to do is update
this pod file and let's open up terminal here and simply type in CD and a space
and just go ahead and drag and drop the root folder of your project into
terminal. Hit return and type in pod update, okay. Hit return again of
course all of my pods are up to date and that this will take a while but go ahead
and wait for it and you would be able to launch the, actually basically the
project you will not have any errors and we are using pod here because we
are using tiny constraints as usual to constrain our views and we are
basically constraining the label and the button pond and that view controller.
So let's wait for this to finish and it should finish in just a moment and then
we will be able to launch our project. Till then I am going to walk you through
this project, okay. Basically we are having a root view controller and that
is added as a root view controller to our window and we adding it to our
navigation stack there and let me just open that up as you can see
it closed this because the update has finished so let's go to terminal yes
indeed it did, okay. Let's go to our root view controller but before we do that we
have here a dispatch queue helper file I want to click into that so we don't ruin
the end of it so yeah that will be helper file once you understand what we
are going to build. So let's go into the root view controller, here we have our a
button or a label and we are setting up the views we are setting the background
color to be white adding the two views the button and the
label and constraining it on our view controller and for the button we have a
selector once we tap on that button we are calling on the button tab and that
button tab is calling grab data from a database. So here we are and that is the
basic of our project, okay. Now let's just start off by knowing
what we want to do. Basically we want to fetch a background thread and let's say
fetch some data on and that. So what you would do is go ahead and type in ... and
here you have some options to choose from and as you can see we have a sink
with a code or execute or you can execute a group or async after we will
talk about async after in just the finishing of this video. So let's just
choose this one: a sync group qos that is dispatch QoS and so on. What we
want to have here this is an optional so we just can simply delete that. Now you
can see that we can choose background default unspecified and so on. So we are
going to choose a background and we are not going to use any flex for now and
execute is the code that you want to execute on the background thread.
So let's say let's print out here ... just a D here.
So let's build and run and see what happens well basically a print statement
will be added to the log nothing really changes. Let's just click on that on the
background thread now and it is instant. Now if we wanted to change let's say the
text of our label like label dot text equals two on the background or
something like that and of course we need to add in here self
because we are in a completion. This setting of the text of the label will be
let's so call it the unpresise. It is not immediate because it's on the
background thread whether the system has and memory or resources it will go ahead
and update the text of the label. That is why we always have to and this is by
default we always have to set the text of the label on the main thread.
But if we are on the background thread we shouldn't set the label text or any
UI changes once we are on the background thread, yes. So let's just build and run
and see if this on the background basically comes up instantly or
something like that. It can vary it can last for a few seconds or it can be even
instant but it's tricky. Let's hit on that and yeah it is still
waiting for result and you get an error UI label text must be used
from a main thread only and as you can see it doesn't really update at all, okay.
What you want to do here is come back to the main thread and yeah this is
basically mimicking the background of let's say a firebase call. So what we
want to go, do is call a dispatchQueue dot main
dot async and I will call this code or execute it doesn't really matter let's
call this code and let's just grab that and paste it in there and let's print
out now we are on the main thread now. Okay and on the main thread because we
are on the main thread now. Now this should be instant so let's just build a
run again and let's tap on the button and on the main thread.
Great, okay or we should go with let's say we fetch the result here on the
background. So let's see let data or let's call it a result equals one.
It will be a simple integer and now we are going to add in here result, okay let me
just check, waiting for result, yes and a result is a there. So let's just build
and run again and we should see result equals to one. So we did fetch the result
on the background thread and we are displaying the result on a main thread.
So there we go. Instant, great, okay. Now we should be able
to let's say wait for a few seconds so let's mimic that too. Now we could do
something like this like sleep let's say for 3 seconds. Now this has its own
issues like sleep really means that we are freezing everything on the app. So we
cannot tap on anything we are just freezing it and this you can
see the result is a one, okay. Well that isn't quite convenient so what I like to use
is async after. So let me just comment all of this out maybe we will
use this later on but let's just build out. So let's type in
dispatchQueue dot global and async after and I will choose and this one deadline
dispatch time and execute. So let's create a dispatch time here lad and
it has some uptime nanoseconds let's add in here three. I believe that should be
great and add in here the dispatch time that we have created and we are going to
execute maybe we want to come back to our main thread now because let's just
copy this out because all of this is inside our dispatch queue background
thread, okay. Let me just uncomment this maybe copy it, comment it out yeah a lot
of copy and pasting here but you get the idea. So let's take that and there we go.
Okay, now this kind of looks cumbersome but it will do the job, okay. Let's build
and run again and see what happens, okay. Let's tap on the button oh and yeah
we need a dispatch time dot now and let's add in here some seconds
and the seconds
need to be of type double and it will be three seconds, so yeah you need to add in
a double as of the dispatch time and now so let's build and run and see what
happens. Yeah and as you can see this is kind of cumbersome so yeah let's tap on
that now as you can see it's in on the background thread and we after three
seconds we did fetch the result and we did set it on the main thread as result
equals one, okay. Now that is pretty awesome but it's kind of a lot of code
and you should have a wrapper around that or maybe let's call it a helper okay and
that is where a dispatch cube helper comes in I have created it and you can
use it to your liking. Now, let's go into that file and see what
it actually is. Well it is called dispatch queue helper and we have set
the initializer the basic initializer to be five private because we don't want
any other people to initialize it. Now we have here a function call and it is a
static public function delay by seconds and of course the second is a double and
the dispatch level is of type dispatch level and it is sad if you are not
adding it as main but you can also edit and here we have a completion, don't
worry about the completion we will be talking about the completion in the next
video. So what is this dispatch level? Well it is the thread that you actually
want to choose. So I read it here some cases with all of the cases the mainly
user interactive user initiated utility and background what you need to know
about is the background and main and it is basically you already know this.
Dispatch queue dot main dispatch queue dot global user interactive and we
have used this background one, okay. And this dispatch level is used and this
function that we are going to call and it basically what it does is set at
dispatch time like we just did in a few minutes ago from dispatch time now and
it adds in the second so that is what we are going to wait for and uses as this
dispatch queue async and with a deadline and we are going to execute this
completion again don't worry about the completion we are going to talk that in
the next video, okay. So how do we actually use this? Well it is pretty
straightforward. Let's just comment all of this out and write our new call and
you will see that it's pretty easy. So let's call dispatch queue helper okay and
delay by seconds and you can see we have a dispatch level option or without if we
don't choose any dispatch level then it is set of course to the default dot
main thread. But for now let's just choose our a background and let's just
delay this by 3 seconds and the dispatch level should be background. There we go
and completion now once this is completed so let's just add in the
result here. So we have grabbed the result after 3 seconds and now we want
to set this result on the labels text property. So dispatch queue helper of
course you could use dispatch queue main dot async in this scenario but let me
just show you how we would delay by 0 seconds so it's immediate and of course
we are not using a dispatch level in this function, so the mean is set by
default and we are going to set the labels text to be resulted.
So let me just build and run one last time and
yeah it's a lot more easier more comprehensive and easier to write. So tapping
on fetch data from server one, two, three, and result equals one. great. So now
you know how you can use grand center dispatch and dispatch queue dot main or dot
background and what it all means and of course you are given a present with
dispatch queue helper it is very easy to use and go ahead and download it and use it
in your code to your liking. Now I hope you enjoyed this video if you did go
ahead and give it a thumbs up subscribe to the channel and hit that notification
bell I dare you and as always I will see you in the next one.