Do you find sorting your CoreData Stack to be really, really hard?
Well not anymore, because in this tutorial we are going to learn all about
sorting and fetching in CoreData. Now I want you to hit that SUBSCRIBE button
and maybe hit that LIKE button if you really did
enjoy this video, because we are going to have a
step-by-step example on CoreData fetching and sorting.
Alex here from Rebeloper open up Xcode
and let's dive in. Okay! So let's just see what we are going
to build out today and go ahead and take a look at
the description where you can find all the links for the
resources and the GitHub repository. So this is what we are going to build.
It's called Catz and we are going to filter and sort all the cats. So if you
just start to add some new cats here, they will be
generated randomly that's why we kind of got Oliver and age 1 kind
of the same and the happiness factor and whether if
they are hungry that's also kind of a property. Let's just add a
new one so you can see that we indeed get a new cat here.
So what we are going to learn today is this new filter
and sort and we are going to filter and of course sort
all of these cats. So that is what we are going to do
and let's just take a look at the Xcode project
so we can better understand where we are at. I'm using SparkUI 0.11.11
just to make this view controller build out, the UI build out
really, really fast. Go ahead you can just use whatever
you'd like just so you know if you are taking a look at
the code on a GitHub. So that's that, anyway we only care about
the CoreData stuff. So if you take a look at the downloads,
the resources. I will have to tell you one important thing here is
that the CoreData stack fetch. Now if you don't know
about CoreData Stack go ahead and check out the link in the description for the
whole playlist on this CoreData Tutorial series, where
you will understand what CoreData stack is.
So right now all I have to kind of explain as an extra is this fetch,
fetch request of type async with the completion handler.
So that is what we are going to use in some parts of our tutorial today.
So what is this async? Well basically when we are
fetching a request from CoreData that is handled on,
well it might take a few seconds and if you are doing this on of
course on the main thread that will freeze the
mainsail thread. So what the they suggest is that we use
NSAsynchronousFetchRequest. So basically that's what we
should do always. So I set this to be as default as true.
Now we want we don't want to use the fetch.
So fetch, fetch request on the manage context, we want to use
execute and yeah basically those are kind of
the differences between those two approaches. So that's the fetch and
let me just take a look, yes. Let's just take a look
at the CoreData Model, okay. So first of all we have our entity.
That's a cat, age, cuteness level, image name, is hungry
and the name. So these are kind of the attributes that we want to add in
order for this tutorial to kind of explain all
about fetch requests. Now as you can see I we
have here a lot of fetch requests already created. Now these
are default fetch requests. We will cover them in just a second.
So we have a cat cell and in the view controller
we just have a simple UICollectionView. Now I won't go detailing all of the
UI stuff, what I do want to show you is that let's just scroll all
the way up and let's just start off by saying that here
we have our cats array and that will be populating
the UICollectionViewController. Now here we have the add bar button item
and the add cat go ahead and take a look at it.
I have discussed this in a previous episode how to add new
elements into our CoreData. So new objects basically.
Filter and sort, bar bottom item. Now here comes the important part for our
tutorial and what I'm doing here is creating
and an alert which is basically a UIAlert controller
in SparkUI. Go ahead and just use a simple UIAlert controller
because as you can see here we have a lot of
UIAlert actions and basically what they are doing is
referring to another function those are down here.
But I will just go ahead and guide you through them
at the appropriate time. Here we have our flow layout, collection view, you don't
have to worry about this. The setup, the set the left bar
bottom and right bar button item, setting the collection view on the view and here
we have our data source for our collection view.
As you can see it's just using the cat array and the cat
cell, okay. Now here we have our add cat, what you have to know
is that we are generating a random cat basically
that's it. And we are saving it, saving the
context after that. We have a fetch cats.
Now that's just general. I have talked about this in previous episodes.
This is just fetching an entity name of type cat and
this is also in the CoreData Stack struct that I have created and you can
take a look at it. It's just simply fetching that type of
entity, okay. And this handle result is basically
just, let just scroll all the way down, it's just
setting the collection view, so we are reloading
on the main thread and we are setting the cats as the result.
Otherwise we just show an alert. Now let's just move a little bit uh upward
and yeah this fetch cats is just
called upon view did load. In our case it's
an inscription of SViewController and it's called on the own load but
basically on view did load, okay. Now here we have
the fetch request with template and let's start off with that
and after that fetch ordered cutest cats and cutest
age cats, basically those are those but let's just start off with
fetch request with template. Now what we are doing
here is let me just jump to this definition on the
struct there. We are basically creating a fetch request with
that request name. Now as you can see this is
persistent store coordinator manage object model fetch request
template and we need this requested name. Now what is this request?
Well this is the easiest way to fetch or create fetch
requests and that is right over here as you
already saw earlier. Let's just take a look at the fetch
request all. Now we have created a fetch all
cats and we didn't do anything here. Now request cute 1 because yeah
cuteness of 1. Now cuteness level and as you can
see we can grab any of the entities here and we can add
in here a value, contains, is, is not, begins with,
ends with and so on. So this is cute1, cute2 and cute3. As you might have expected.
Now or we also have fetch request hungry
and that is is hungry is 1, old, age is greater than or equal to 3.
Now again this depends on what type of fetch request you want. These are just
pretty examples that you might want to consider taking a look at.
And the fetch request young is age is less than 3. So those are
my predefined fetch request on the CoreData model. Now how do you create one?
You just long press on this button add entity and just long press and then
you can just simply scroll down, I don't know you can't really see this
but you can just scroll down or up whatever, I believe yeah you just need to
scroll there to add a fetch request. So add a fetch request,
that is what you want and then you can with that name, now the
important part that with that name you have to call
that name exactly and then you can just use that fetch
request to your liking with the template. Now let's just
go ahead and go here, so here is our persistent store coordinator
manage object model fetch request template. Now go ahead and
use this public function to your liking because it's
much more easy to call that. Let me just go all the way up.
There we go. Let's just take a look, yeah there we go. Fetch request with
template and fetch request hungry. So that's just that.
Now here we have with the filter of one cuteness level
and that is a fetch request cutet1, cute2, cute3, young,
old and basically those are that with the fetch request
with template and of course I have already created fetch request template
with fetch request all, but these are just
sorts. So before we move on let me just show you
how all of those predefined template fetch requests
works. So let's just see here: filter hungry.
So, there we go. We have all of those and as you can see it's
of course updating automatically. Now filter
one cuteness. Well we don't have any with the one cuteness filter two.
There we go and as you can see it's a hungry,
well all of them that are with cuteness level two
and cuteness level three. We only have Max there, okay.
And finally filter young. So below age of 3 and I don't think we
have old here but yeah we don't have old but you just go
ahead and create random cats there and you will find those, okay.
And finally let's just show all, there we go it's just
showing all, okay. So next up will be this sort by name, okay.
So let me just take a look at the code here. So we are using this fetch request with
template, fetch request all. So we are getting
all the cats from our CoreData Stack and then we are
applying a sorted on the cats array.
Basically what we are doing is grabbing cat0,
cat1 and we are just grabbing their names
and basically just sorting them by a name with this line of code
and of course we are doing the collection you reload
right over here because we, well basically we are not doing
the anything to add onto those cats.
Like we are not triggering that. So yeah that's what we are doing here.
Sort by name Z-A and that's just reversing the I don't
know what it's called it's a duck sign, yeah you know.
And finally here we have fetch all.
Now we could just use fetchCats, you know the old
type of way of doing this, but we can just
call fetch requests with template and fetch request all.
So let's just take a look at the ordered
cutest action. So it will be ordered by A-Z
and of course by age.
So let's just take a look at those .functions
Now this is kind of complicated but once you get a hang of it
it will be really, really easy. So here we have a compare selector and this is
the localized standard compare. Now what this
basically does is that it lets strings to be
compared. Now here we have our sort descriptor and our predicate. Now the
sort descriptor is basically grabbing the cat's name and setting it,
giving us back in ascending order.
Now the predicate. his kind of looks, kind of
strange to you but just so you know here we have our
key and here we have our value and here is our key, the cuteness level
and with the comma here we have our value and then we
create a fetch request as usual add the sort descriptors predicate and
then we fetch on the CoreData Stack, okay. Now let's
just take a look at the ordered cutest and age. Now this is just
to let you know that we can have more than one sort descriptor, that's why
it's called sort descriptors and make sure that
you order the sort descriptor as you'd like
for it to be used. Basically that's it. Let's just build and run and see
how this looks like, okay. Let's just add some more here.
There we go and the filter and sort ordered A-Z, okay.
So that's that and ordered by age and of course the cuteness level
should be 3. 4,3,1, okay. Really, really nice, okay. And basically
that's it. Make sure that you use this kind of programmatic
fetch request. If your fetch request is kind of dynamic you might want to
have this cuteness level as a dynamic one
and you could also cut time on development if you are using
the like template fetch request. These are used whenever you know that
okay you are going to use this a lot of times and it will
not change meaning the properties of our fetch request and that
is all about fetch request. I know it sounded a little
bit scary but now you know that it is not, it is
indeed really easy to use fetch request with
CoreData. So now you know how you can sort and filter your CoreData.
How awesome is that. Now that is it for our CoreData Tutorial series.
This is the last episode, if you haven't
known already this is a tutorial series and go ahead and check out the link in
the description for the whole playlist. Now if you go through each one of these
tutorials, then you will ace CoreData and why is
that important? Well it is important because that is one
of the main persistent stores from Apple.
So if you're looking for a job or you just want to broaden your knowledge
in the Apple iOS ecosystem it is imperative that you
do learn CoreData. So go ahead and check
out the link below for the whole playlist. Now I will be posting
more videos about iOS stuff and while you wait for that
go ahead and check out these videos as well too. I talk a lot about
Swift, SwiftUI, UIKit in general and as usual I will see you
in the next one.