Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Once we create a fetch request, to actually get the data, we need to tell Core Data to perform the request. In this video, let's execute the fetch request and work with the resulting data.
Resources
-
0:00
In the last video, we created a FetchRequest.
-
0:03
All that's left to do now is to perform the FetchRequest and display the results.
-
0:08
The FetchRequest is performed, or executed, by the managed object context.
-
0:13
So in our TodoListController subclass, let's get a reference to that context and
-
0:19
assign it to the stored property, to a stored property rather.
-
0:23
So the top we'll say let managedObjectContext
-
0:28
equal CoreDataStack.managedObjectContext.
-
0:34
Now once we have the context, executing a fetch result is quite simple.
-
0:39
We'll use a throwing method to do this.
-
0:41
So inside viewDidLoad.
-
0:45
Actually before we do that, we need to import CoreData and
-
0:49
then back inside viewDidLoad, we can say, let request equal
-
0:54
Item.fetchRequest, because this is created as a static stored property.
-
1:01
So if we go back here,
-
1:02
you'll see that this is a class function rather than a stored property.
-
1:06
So, there's an annoying bug though here.
-
1:08
It's not really a bug, it's an issue that pops up where we have to specify the type
-
1:14
of the fetch request to get the compiler to understand which method we're calling.
-
1:18
So here we'll say NSFetchRequest, which is generic over item.
-
1:23
The reason we're doing this is since there's a similar method in the base
-
1:28
class, remember I said that there is already a fetch request method in the base
-
1:32
class, which is why we put this @nonobjc here.
-
1:35
And because there's that similar method,
-
1:37
the compiler is going to complain that just calling fetch request is ambiguous,
-
1:42
because it could be either one of those methods.
-
1:44
Since one of those methods returns a non-generic request,
-
1:47
if we specify the type of the constant as a generic one,
-
1:51
the compiler then knows which method we're trying to call.
-
1:54
Now, you could avoid this dance by getting rid of the generated method.
-
1:57
For example, back here we could just give this a different name, but
-
2:01
we'll stick to this just so that you know what this bug does.
-
2:05
Well it's not really a bug, but okay.
-
2:07
So let's use this request to execute a fetch.
-
2:10
The fetch method on the context is a throwing one again.
-
2:13
So we need to stick this code in a do catch.
-
2:16
So do let items equal try managedObjectContext.fetch.
-
2:23
And this takes a request, we'll pass in the request.
-
2:26
Now, if an error occurs that is propagated to a catch statement, so
-
2:29
in here we'll just say print error fetching item objects, and using
-
2:37
string interpolation, we'll just print the error's localized description string.
-
2:41
Okay.
-
2:42
The fetch method returns an array of item instances so
-
2:46
let's display these in the table view.
-
2:49
We'll start by adding a stored property to keep track of these items.
-
2:53
I will assign an empty array as the initial value, so we'll say var items is
-
2:58
or equals an empty array of item objects.
-
3:04
Let's also add a didSet observer, so that after initialization,
-
3:09
when a new value is assigned to this, we just automatically reload the table view.
-
3:13
So didSet, tableView.reloadData().
-
3:18
Now standard stuff, we've done all of this before.
-
3:21
Now, down here in viewDidLoad instead of assigning the results of the fetch request
-
3:26
to a local constant,
-
3:27
we'll assign the results of the fetch to the stored property.
-
3:30
So all we have to do is get rid of that let.
-
3:33
Okay, now that we have items in a stored property, let's provide implementations
-
3:38
for the table view's data source method to actually get these out on the screen.
-
3:41
So I'm gonna get rid of any unnecessary code here.
-
3:45
So number of sections in the table view return 1, and get rid of this comment.
-
3:51
Numbers of rows in sections, we'll say items.count and
-
3:56
then so for index path is commented out.
-
3:59
Let's get rid of all of this first, a bunch of stuff we don't need for now and
-
4:05
we're going to get rid of the comment markers.
-
4:12
And in here we'll grab a basic cell, so dequeueReusableCell with identifier.
-
4:18
We'll say ItemCell as the identifier.
-
4:22
And we'll need to go back to the story board and adjust that, so that we know
-
4:25
what cell we're looking for and then we also need to get an item object, so
-
4:29
we'll say let item = items[indexPath.row].
-
4:34
If you're unfamiliar with what I'm doing here,
-
4:36
you need to check our table views course.
-
4:38
So what I'm doing here is using the row property on the indexPath
-
4:42
to figure out which row we're on, and
-
4:44
using that as an index value to get an object out of the item's array.
-
4:48
Once we have that, we'll say cell.textLabel.text = item.text.
-
4:54
Okay, and return the cell.
-
4:55
Now, we specified an identifier here that is, as of now, undefined.
-
5:00
So let's duck back into main.storyboard and
-
5:04
we have a table view in here with a prototype cell.
-
5:08
So let's select that.
-
5:09
And in the attributes inspector, we'll set this as a basic cell and
-
5:13
then give it an identifier of ItemCell.
-
5:16
Okay, so this is the text table that we're assigning the items text property to.
-
5:23
Let's run the app.
-
5:25
And hey, look at that.
-
5:27
A single item should be displayed in the list.
-
5:29
This is the item I added earlier.
-
5:31
There is an issue, however.
-
5:33
So let's add another item to our list.
-
5:36
So I'll say, buy groceries, and save.
-
5:42
So we didn't crash, once we were done we hit save.
-
5:46
We know that the save worked because there's no errors, but
-
5:50
there aren't any updates to the list.
-
5:53
There's a problem in our current approach in that we have to always be listening for
-
5:57
when a user saves or dismisses that modal and then perform the fetch again and
-
6:01
then reload again.
-
6:03
That requires either the view controller knowing about what the other one is doing,
-
6:07
about what the modal view controller is doing.
-
6:09
And then we get into that coupling territory that we don't want.
-
6:12
Luckily, we don't have to do any of that.
-
6:15
In the next video, let's take a look at a fetched results controller and
-
6:19
understand how this object is perfect for the job at hand.
You need to sign up for Treehouse in order to download course files.
Sign up