Now, we still have a rather fundamental problem with our app. And that's when we open up a new app, 1
we didn't get anything loaded up in our table view. 2
Despite the fact that when we add items, we're actually able to save it successfully into our persistent 3
container. 4
So let's fix that by targeting the next word in CRUD which is read. 5
So how can we load up items from our system container? 6
So let's go into our load item's function and let's uncomment it. 7
But we're going to delete everything that's inside because we're using something that's completely different 8
from encoding and decoding. 9
But instead, we're going to create a new constant called request and we're going to specify its data 10
type as NSFetchRequest that is going to fetch results in the form of items. 11
So in Swift, there's very few cases where you actually need to specify the data type. 12
In most cases, you specify the data type because it helps you with the programmer or people on your team 13
to be able to easily see what is going on in your code. 14
But in the majority of cases, Swift is clever enough to figure out what is the data type just based on 15
the value that you give it. 16
But in this case, it's a little bit different. 17
You actually have to specify the data type. 18
And most importantly, the entity that you're trying to request. 19
So we're going to say let request be of data type 20
NSFetchRequest, that's going to fetch a whole bunch of items. And then we're going to tap into our 21
Item class or Item entity and we're going to make a new fetchrequest. 22
And, of course, as always, our application have to speak to the context before we can do anything with 23
our persistent container. 24
So we have to say context.fetch. And the fetch that we want to make is our current request which is 25
basically just a blank request that pulls back everything that's currently inside our persistent container. 26
Now, the problem with this is that similar to context.save, context.fetch can also throw an error. 27
So we need to market with a try and then we have to make it happen inside a do catch block. 28
And if there were errors, then we'll print "Error fetching data from context" 29
and the issue will be printed using error. 30
And finally, you can see that this method actually has an output and the returns NSFetchRequest 31
result which we know to be an array of items. 32
So, as you can see, there's quite a lot of logic to trace, and that's why Swift tells you that you must 33
specify the data type here. 34
If you don't and you try to run your app, then you're going to get an error that says, "Ambiguous use of 35
'fetchRequest()' which, to be honest, is quite an ambiguous error by itself. 36
But just remember that you need to specify what is the data type of the output. 37
So we know that the output for this method is going to be an array of items that is stored 38
in our persistent container. 39
So what have we been using to store all of those items in the interim? 40
Well, of course, our item array. So we can say itemArray = try using the current context to fetch 41
this broad request that basically ask for everything back. 42
And once you do, then you're going to save the results inside our itemArray which is what we used to 43
load up our table view data source. 44
So before we run, let's make sure that we uncomment this loadItems inside viewDidLoad and let's run 45
our app. 46
So as you can see, when our app loads up, it's already fetching all of the items inside our database. 47
And if we go into our database and we do something, say, let's just delete the last entry, hit the minus 48
button, 49
Delete Row. 50
Yes. 51
And now if we terminate our app and open it again to reload 52
viewDidLoad, then you can see that it's reflected over here as well. 53
Now, the reason why it doesn't update automatically is because we manipulate the data inside the database 54
directly. 55
There's no way for us to be able to reload the data in the table view because we can't call 56
tableView.reloadData from the database, 57
right? 58
And later on, we'll talk about deleting items, then we can look at how we can update that automatically. 59
But for now, we've assured ourselves that our loadItem function works and it fetches all of the results 60
that our items from our permanent stores through our context. 61
So the next thing that we might think about addressing is the"U" in crud, right? Updating. 62
How do we update data inside our database? 63
So that's what we'll do on the next lesson.