Everybody this is Caleb with Debb slopes.
And in this video we're going to use our managed object context to create a fair request.
That's going to poll data that we've saved in our persistent store.
Really really cool stuff.
Then we're going to fill our array or we're going to create an array fill it with those goals and then
later on we're going to use that to actually pull in that data to our table view.
So go ahead and pull open your X code project load up goals VC and now we're going to go ahead and create
functions that are going to fetch all of our gold data.
Super super cool.
Let's go ahead and do that now so we're going to actually create an extension of goals VC like so by
typing extension goals VC or goals VC.
And inside here we're going to write three functions.
Now we've got to make sure that we have core data important.
We do.
So that's a good thing.
Let's go ahead and let's write this function.
It's going to fetch all of our gold data and then save it into an array which we're going to create
and maybe two minutes.
So go ahead and type phunk fetch.
And we're going to go ahead and create a completion handler just like we did with save so that we know
when we're done.
So go ahead and type completion and the completion is going to happen inside of here.
OK.
And what we're going to do is we're first going to set a constant here called complete and it's going
to be of type Boolean and this closure which is a function is going to basically return to us an empty
function so that we can just pass in a value to here.
And then when we call fetch we get a value return to here and then we can do something inside of the
closure completion.
So really cool.
Here's what we got to do in order to get that data from our managed object context.
We're going to basically first create a constant to hold the managed object context.
We're going to create a fecche request which is a part of grabbing data from the persistent storage
coordinator.
Then we're going to basically use our managed object context to fetch all that data through the fetch
request and then set our array full of goals.
OK so let's do that.
Let's go ahead and type.
Guard let managed context equals app delegate dot persistent container dot view context.
If we don't have that else we'll just return.
OK so assuming that we have our managed object context which more than likely we will we're going to
move on and create what's called a fette request.
So let's go ahead and name it that let fecche request equals and ask fetch request.
And what we're going to do is we're going to set a type of fetch request meaning what should we be fetching.
Now this is going to be of type and s fetch request.
And if we put a parentheses we can see that we can call an entity name.
Now the interesting thing is if we go to our X-C data model our entity is named goal.
So if we go in here we can pass a string of goal.
So now it knows that we are trying to fetch items that are of this particular entity.
Very very cool.
Now what we need to do is we need to call managed context fetch and pass it the request but do you notice
that it throws.
That's our clue that we need to use.
Do try and catch.
So we'll just enter this and we'll pass it the fetch request but we need to call a do try catch block.
So go ahead and type do.
And then down below we might as well just type catch because that's where we'll handle errors.
And now we're going to go ahead and paste this in here.
Managed context not fetch.
But like I said we need to go ahead and call try.
Now do try and catch our what we use in order to do things that can throw errors and if they do we're
going to use Debug print debug print apparently I can't type Debug.
Print.
There we go.
And we're going to go ahead and print could not fetch and we'll pass the exact error from our.
Do try and we'll give it the localized description there so that we know exactly what it's talking about.
Very cool.
And so this is great but I want you to notice for this function fetch if you let's call it again fetch
you'll notice that it returns to us an array of N.S. fetch request result.
So if we pass in our fetch requests like that it's going to return to us an array of whatever request
we asked for which in this case is goal.
So if this function returns to us an array of all of the results that it gets from its request we should
create an array of goals and then set this to fetch and pass those into our array.
So let's go up to the top and beneath our table view let's go ahead and create an array.
So type var goals which is an array of type goal OK that's easy enough and we're going to just instantiate
it from the get go to be empty.
OK that's easy enough.
But down at the bottom when we get all of our managed context fetcher Quest results we're going to go
ahead and set goals to be equal to this because the try here whip's goals try here is going to basically
call fetch return an array of the results which we know are going to be of type goal.
So we can append them directly to our goals array Super-Duper cool.
And now that that's done once we successfully do that we can actually call our completion handler so
we can say hey we're done.
And you know while we're here we may as well print successfully fetched data.
OK.
If there is an error we're going to say could not fetch and will call completion false.
OK so we're using our completion handler there that's great.
And now here's what we're going to do.
We need to think about when should we be fetching data when should that happen.
Now if you did load is great but it's only called once when the view originally loads.
And if we have presented our create goal the see our finished goal we see if we've presented those on
the top and we dismiss them feuded load is not called because we're just presenting a view on top of
it and dismissing it.
What we can do is we can put this in view will appear because when the view appears that is because
whether the app loads initially whether a screen is presented and then dismissed view will appear is
going to be called every single time the view appears not just when it loads.
So we're going to call view will appear because that is actually the perfect place to put this call
super.
You will appear and passen animated.
So now we're going to go ahead and call self not fetch and we're going to pass it a completion handler
here.
And let's just say complete and if it's complete We're going to go ahead and do a check to see whether
or not we should show or hide our table view because if we fetch all of our data but we get zero results
we still should not show our table view remember that it's hidden from default.
So we're going to go ahead and see if goals count is greater than or equal to one meaning if there's
at least one we're going to go ahead and say table view is hidden equals false meaning table view is
showing if there's more than one result that's brought back our table is going to be showing.
But if that's not true else we're going to go ahead and say table view is hidden equals true cause if
nothing is returned our table you should definitely not be showing.
Kate that's pretty easy.
So when the view appears it'll fetch the data it'll load the table view and it will show it or hide
it if it needs to really easy.
So that's awesome.
But now we need to go ahead and think about our table view.
It's not going to be able to load the appropriate information into the table view because we have it
set here to only load a static three cells.
So in order to get the proper count for goals we're going to actually pass it goals count.
If we had four items we would have four rows.
Okay.
Very cool.
The same thing goes for the cells we're not properly setting up the cells here because we're not properly
accessing the goals.
So we need to go ahead and do that by creating an instance of goal like goal and we're going to go ahead
and pull out each goal at the index path for each cell.
So we're gonna go into the goals array and we're going to pull out the item at the index path daro.
And if you've ever worked with a table view and models you understand how this works.
If the index path is one it's going to pull out the item at index Cathro one that's super easy.
And as you can tell goal is going to be of type goal because it's coming from Arkell's right.
Okay now what we're going to do is we're going to pass in all the information we need for each cell
we passed in a static title here.
But what we're going to do now is actually type goal datt goal description.
Okay.
And that's where we're going to actually get the data from that specific goal the type we're going to
get from gold gold type.
All right.
The progress amount we're going to take from goal dot goal progress or let's say goal progress amount
because that doesn't initially have a value let's think.
Well no you know what it needs to show the goal progress because the goal completion value was actually
saved.
When we create the goal the progress values always 0 from the beginning.
So it should start at zero because as we add one little increment the number that makes way more sense.
So very cool.
That looks awesome.
And we have now officially configured the cell phone our goals array I think we should build and run
it to see if it works.
OK let's build and run.
Let's see if our fetch function is going to successfully work.
And it looks like we're getting some errors here.
Interesting.
So let's go ahead and find out what the deal is.
We get one error here.
Oh of course it is not unwrapped.
We have an optional type of string from goal description so unwrap that.
Now it's saying that generic parameter result type could not be inferred.
OK.
So we just need to set an explicit type for this as fetch request.
We're telling it to fetch goals but we need to actually tell it that we explicitly need the type goal
like.
So by putting it in the the carrot's there that tells it that we're asking for the type gole specifically.
So that's great.
All right so it's giving us an air here cannot convert value of type string to expected argument type
gold type construct gold type.
We don't want to do that.
We don't want to make it into a gold type when just want to pass in the string value.
You know what I almost wonder if it would be easier if we went into gold sell and instead of passing
in these values like this what if we just passed in a goal of type goal and instead of passing in a
specific description value we could just say goal doggle description we could say goal datt type K because
it comes in as a string.
That's better.
And then for this one we could go ahead and type goal dot goal progress.
Now I think we're still going to have to put that into a string like so remember it's string describing.
OK so that should still work OK.
Nothing's wrong here but we are definitely going to have an error here.
Now it's going to be easier on this end because configure cell now just asks for a goal and so will
pass it that goal.
And now we shouldn't get any errors.
We should be able to properly fix that.
Let's go look at gold type just to make sure.
Or go sell I mean.
Yeah because gold type comes in from our bowl is a string because when we save it we save it as a string.
Awesome.
So very very cool.
Let's build and run this let's see if it works and yeah let's check it out.
OK hey look at that.
This is actually these are the ones that we've already saved.
Let's try saving another one.
Dance twice today just in case you feel like getting funky.
All right Preston next.
How many points until complete let's say three.
And if we did this properly when Goles VC appears meaning when this dismisses it should fetch all of
our data and it should set our table view to be showing or hiding so it's saying that it successfully
fetch the data.
But I'm not seeing it updating here.
That's interesting.
I wonder if we were to build and run it again if it would show up because I think that we're not yet
reloading our table view like we need to.
OK.
Yes look at that.
So it's there but we just need to reload our table view and you know what I think.
Let's think so.
If goals don't count as greater than 1.
We want the table view to be showing.
And you know that would also be a great place to reload the table view.
OK well no you know what.
Because what if there's less than one.
We also need to reload the data because if one were to be deleted.
So you know instead of putting it.
Here how about every time the view appears we're going to reload the table view then we fetch the data
or no you know what.
Even better let's fetch the data and then at the very end we will reload the table view no matter what.
So that whether it's hidden or showing if there's more than 1 or 0 it'll reload every time.
So let's try this out.
Let's create a goal go to the park twice a week and let's do that for three weeks.
Create goal.
There we go.
OK.
So it shows up.
Awesome.
I am noticing a problem we are having some issues here with the text labels being out of place.
So let's go fix that really quick.
In main story board that does not look pretty.
Ok so yeah it's probably an issue with our stack view here.
And you know what let's see so goal is set this is set.
And how are we filling this filling proportionately in case that's going to be a problem if we have
smaller text.
So let's go ahead and let's fix this so that it's not going to move anywhere.
What we're going to do is we're going to change this to fill and fill and then what we're going to do
is we're going to set a goal to have an explicit with so that this always is going to be the same distance
from goal.
So with the goal label selected click the pin menu select with and we're going to make this 50 y.
Let's see if that works ok.
Awesome.
So let's go ahead let's build and run this let's see if that fixed our problem and we should be good
to go.
Guys we are loading data from core data we're pulling it from the ones that we've saved.
Oh yeah look at that.
That looks way better.
We did mess up this one at the bottom but we can fix that by also giving type an explicit with let's
just make it 40 and then let's build and run again.
Let's see if that fixed our problem.
It should unless we're not actually using the right stack.
OK.
Look at that.
That looks perfect.
So we're pulling the data we've saved long term short term titles titles the the completion everything
is showing exactly as we want.
This is amazing guys awesome awesome work.
We filled an array.
We filled our table view with data from that array and then the next video we're going to write a function
that's going to allow us to remove objects from core data and allow us to swipe and delete them.
Then we're going to go ahead and add a really nice animation so when you press delete it animates up
no matter which row you choose to delete it's going to be super cool.
And let's go build it now guys we're so close this is amazing.
Let's move on to the next video.
Amazing work with this one.
I'll see you there.