Async Await tasks are really, really nice but what if you want to have multiple tasks at the time?
Well in this tutorial I'm going to talk about that. I'm Alex from REBELOPER.COM and if you like these
videos go ahead and SUBSCRIBE to the channel and while you are at it let's open up Xcode and
let's dive in. So you know how to add an Async Await task but what about setting up multiple
tasks with Async Await? Well that is what we are going to learn today. Before we dive into the code
let me remind you about the DEV_FACTORY, it is at rebeloper.com/mentoring where you can meet with me
on a 60 minutes one-on-one Zoom call where we can discuss all of your coding issues. Maybe you have
a bug in your project or if you want to learn SwiftUI go ahead and check out rebeloper.com/mentoring.
Quick note! This video is sponsored by WebViewGold. With WebViewGold you can convert
your web app or website into apps for iOS and Android. I get a lot of sponsored requests, most
of them I decline but WebViewGold stood out. There's no coding required.
All you have to do is set up your URL and optionally customizations. You can add here in-app purchases,
push notifications. Check them out in the link in the description. Save 25% with their back to school
sale and now let's get back to the video. So that out of the way let me just show you what we are
going to build out this today. So we are going to actually create an Xcode project that I already
created, a brand new one. So this is an untouched SwiftUI Xcode project and we are going to learn
how to have a data task, a group task, sorry. So we are going to add some APIs because yeah
I don't want to set up an API. So go ahead and check out the link in the description for the resources
and all you will have to do is just drag and drop this one file into your project. Let me just
drag and drop there and let me just talk you through really, really quickly
what this API is about. So it's just fetching some numbers of a certain count and in a certain
range and it is fetching those numbers in an Async Throws manner. So it waits for the numbers
to be fetched and what I'm doing here is having a Dispatch Queue Main Async After with a deadline
of 0.5 seconds and grabbing some number random numbers with that count and in that range
and resuming with a continuation with checked throwing continuation. So from a completion type of
workspace we are now in an Async Throws, Async Await workspace. So yeah just so you know
I do have a special YouTube video on this. It's one of the previous ones. Go ahead and check it
out how all of this work. Is really, really nice. So that's our API. We are fetching random numbers for
a specific count in a specific range, okay. So what are we going to do to make this learnable?
So first of all let's say we have an example for this. So we have let's say an array of UIDs
that you want to fetch from the server. So you do have that array but you want to fetch them
with the task. so with Async Await. So you might just go with through that array
with a for each or a for loop but you will soon find out that will not work. So that is what we are
going to learn today. So let's just start coding first of all we are going to kind of fetch that
array with that random numbers and we are going to fetch as many other random numbers as that random
number in that particular array. So if it's just three the first element of that first array is three
then we are going to fetch three random numbers. The second one is five, five remnant numbers.
You will see that in just a sec. So we have the Content View right over here. So let's create our
Content View Model. So let's create a new file and name it Content View Model, there we go.
Now of course this is an Observable Object. So let's import SwiftUI first. SwiftUI, sorry for
that auto completion and let's have a class of Content View Model and the super class will be
Observable Object. We are going to create a fetch function. So func fetch and this is an Async Throws
function. Meaning that we can wait for it and it might throw an error. And basically that's it for now,
let's just connect it with our Content View. So let's just go back to our Content View and right
over here let's just move that out of the way so we have a little bit more space. @StateObject
var so it has to be of some type of state object. Var view model equals so we are
initializing it our Content View model, okay. So that's our view model. OnAppear we want to fetch.
So dot onAppear, we want to perform something and if you take a look at another of my YouTube
videos where I detail about Async Await you will know that we need to create a task right over here
and because it might throw that fetch function in the view model might throw, we want to have this in
a do catch statement. So we are going to try to await for our view models fetch function.
Okay! Snd on the catch part we are just going to print out the errors localized description.
So print error dot localized description and basically that's it for our Content View.
Let's go back to our Content View Model and let's just fetch our first number. So we do have our API.
So let's just try to fetch our numbers let's say 10 numbers. So let first numbers.
First numbers equals try Await so we try because it might throw and we await because it's an
async function, okay. Try await API dot and fetch numbers. Now the count would be 10 as I told you
and let's just have this as one decibel. Well one character numbers like from 0 to 9.
So 0 ... 9. So that's our range. That's our first numbers. Let's just print them out
first numbers. Let's build and run this project and let's see if the first numbers will be printed out.
And hopefully they will in our console. So let's just wait for this to build and
there we go. We have our 10 random numbers. Really, really nice, okay. So what we want to do next
is learn the essence of this tutorial and that is for each of these numbers I want to fetch for
example the for the first one I want to fetch seven other random numbers for the second one
four other random numbers. So you might want to go with the four each for the first numbers but
that will not work because we are in an Async Await environment. So instead we are going to
go with a task group. So let's just do that. Let me just comment this out and let second numbers
equals and we want to first of all set the type of these second numbers and that will be
an array within an array of type Int and then try Await. Again try Await
and with throwing task group. Now you could just go with the task group if it doesn't throw for you.
But I'm just telling you that this version because yeah most probably your functions
will fro also. So that's that, let's just stop this so we might have a little bit more.
So what is the child task result type? Well that is actually an array of Ints dot self because yeah we
want to have seven random numbers. So that's an array. And inside the body we want to go in out
throwing task group. I'm just going to name this group and here we have our code.
So let's just type this out it's relatively simple, you will better understand this once
you see the whole code. So first of all we want to have a kind of a return of those
array of arrays. So I will just name this return second numbers, there we go and that's
an empty one. So of type Int and let's initialize that. So this is what we are starting off with.
So now we can go through the first numbers
with the for each loop. So first numbers dot forEach and then first number, first
number, there we go, in and instead of doing the Async Await stuff of fetching a new number
from the API we are going to add a task to our group. So group dot add task, there we go. Add task,
you can set the priority. I don't really care about the priority for now. Let's just delete that and
here we have our operation. So we want to here return some sort of a task. So we are returning
a try await again the API dot fetch numbers is a throwing Async function. So here I'm going to
have a count, well how many numbers do I want, well as much as the first number is and then the close
range would be like 10 and then 99. So that would be kind of we can distinguish that, okay. So return
here, let's fix that typo, okay. So now that we have our groups, our task added to our group
for each of the numbers. Then we want to go with a for loop. So for try await
second numbers in group. So now we got our group and it will give us back second numbers.
The first second numbers, the second second numbers, the third second numbers and that second
numbers we want to append to our return second numbers. So let's just go return second numbers dot
append and second numbers, okay and finally we want to kind of construct all of this. So now we have
our first numbers and our return second numbers. Well actually yeah we are still in the second
numbers with throwing task groups. So what we want to do is first of all return our return
second numbers. Now we want to construct it. So at this point we might want to print out
the second numbers but I'm just going to make it a little bit more readable. So for i in
zero till the first numbers count of course -1 I'm going to go and print it out like so.
So print out the following. First of all I want to number it. So I just go i + 1 of course because
i starts with 0 here and then let's add a dot here so this would be the first, second and so forth and
then I'm just going to go first numbers at i and then let's just add a dash here and second numbers
i and basically that's it. Let's build and run and see how this will be printed out and it
will look really, really nice. As you can see the first one is three. Three random numbers grabbed.
Four. Four, seven and so forth. So this is actually what we are expecting here.
Again what you want to learn here is this with throwing thoughts group or with thoughts group that okay
you have an array of some let's say UIDs or in this case numbers and then for each of those
you want to perform an Async function with of course an Await or maybe it is throwable
like it is in our case. So that is it. Now you know how you can use with throwing task group on
multiple Async functions. Now if you do enjoy these types of videos I highly encourage you to take a
look at DEV_FACTORY. A one-on-one 60 minute Zoom call with me. Just check it out at rebeloper.com/mentoring
where I can help you with your coding issues or maybe you want to learn SwiftUI or
Swift in general. You can hook me up on a Zoom call again it's at rebeloper.com/mentoring. Now that is
an awesome way to handle multiple acing functions at a time. So if you do like these videos go ahead
and hit that LIKE button because it helps a lot. Go ahead and share it with your developer friends
and while you are at it make sure to hit that notification button to get notified of new videos
as I roll them out. New videos like these ones, go ahead and check them out. They are really, really
cool, they are about SwiftUI and Swift in general and as usual I will see you in the next one.