Now, we've come across a lot of new words that actually all describe the same concepts that we're really 1
familiar about. 2
So for example, in the object-oriented programming world, we have something called a class, and that in 3
the Core Data world is known as an entity. 4
And when you think about databases, that's simply just a table that represents your data. 5
And in the other case, when we talk about properties in object-oriented programming, it's actually similar 6
to an attribute in Core Data. And in your database, that would simply just be a field or a particular column 7
of your table. 8
So if we look at one of our tables from our bakery, for example, the buyers' table, the table itself is a 9
class. 10
And if we're using Core Data, then the table would be called an entity. 11
Now, each of these columns that have headings like address, number, who to invoice, or the buyer name, 12
these are field in our database. And if we created the table as a class, we would call them properties associated 13
with the class. But in Core Data, they're known as attributes. So every single row here is a Core Data 14
attribute. 15
So the address would be an attribute to the buyers' entity. 16
Now, if we fill up this table and we give each of these properties a value, then every single row we have 17
in our table would be a brand-new NSManagedObject. 18
So there's a lot of new terms in Core Data. But just remember, when we're talking about entities, we're 19
talking about the class and we're talking about the table. When we're talking about attributes, 20
we're talking about the properties of the class or the columns in the table. 21
And when we're talking about NSManagedObjects, then we're simply talking about every single row in our 22
table that is filled with data. 23
So let's zoom out and have a look at our three tables that we saw in the beginning of this module. 24
We have a Buyers' entity. 25
We have a Products' entity and we have a Orders' entity. 26
Now, if we put all three entities or all of our tables into permanent storage, then that becomes our 27
persistent container. And this container is simply a SQLite database that stores all of our tables 28
and all of the relationships between the tables. 29
Now, when we're writing our app, we can't simply interact with the persistent store directly. 30
We have to go through an intermediary which is known as the context. And this context, as I said before, 31
is kind of like a temporary area where you might create new pieces of data that you want to add to your 32
database or reach data in your database, or specify the things that you want to update in the database, 33
or specify the things that you want to destroy in the database. 34
So this is what's known as CRUD. 35
And when people talk about databases, they tend to use this word quite a lot. But it simply just means 36
all the things that you tend to want to do with a database, right? Create, read, update, destroy. 37
And the important thing to remember is that in Core Data, you do this inside the context. 38
You don't do this directly to the persistent container. And inside this intermediate area, what you can 39
do is you can undo the changes or redo the changes. 40
You can simultaneously add and destroy and update. And only once you've decided that you're happy 41
with what you've done inside the context, 42
you save the context. And the Core Data framework takes care of committing the current state of data 43
inside the context to your persistent stores. 44
So as I said before, this is really similar to the Git example when. When we use Git, we first had to add the 45
things that we wanted to commit to source control to a staging area, and that's when we use the add command. 46
Only when we were happy with what we wanted to commit, 47
did we then use the commit command. 48
So in Core Data, we have a persistent container and our app is not allowed to interact directly with a 49
persistent container. 50
Instead, it has to go through this temporary area which is known as the context. 51
And when you're happy with what's in the context, do you then save the context which basically commits 52
those changes to our permanent container. 53
So let's review our code. 54
So far, we've only implemented the C in CRUD, or the create, inside create-read-update-destroy. But in order 55
to do that, we've actually had to come across all the things that we spoke about earlier on, including 56
the context and the persistentContainer. 57
So the first thing we create is a constant called context 58
and this goes into the AppDelegate and grabs the persistentContainer. 59
And then we grab a reference to the context for that persistentContainer. 60
So if we have a look inside our AppDelegate, we have this lazily loaded persistentContainer, so it only 61
gets loaded up once we actually retrieve it or try to use it. 62
And it's basically a new container that's of the type NSPersistentContainer, and it's 63
created using the structure that we've specified inside our DataModel over here which has one entity 64
called Item, and Item has two attributes: a done and a title. 65
Then we load the PersistentStore and get it ready for use. 66
Now, we access the Persistent Stores' context which is that temporary area if you remember, that our app 67
is going to talk to. Next, when we add a new item to our table view, 68
we create a new object of type Item. And remember, this class gets automatically generated when we create 69
a new entity with that name 70
inside our DataModel. And that class already has access to all the properties that we have specified 71
as attributes: title and done. 72
So we create our newItem and that item is an object of type NSManagedObject. 73
So if you remember we said earlier on, NSManagedObjects are essentially the rows that are inside 74
your table. And every single row will be an individual NSManagedObject. 75
And then we fill up all of its fields, 76
so the title field and the done field. And once we've done that, we save our items. And inside the saveItem's 77
function, 78
we have this method that can throw an error, 79
so it's marked with a try and we try to catch the error if there are any issues. 80
But what it does is that it looks at the context, that temporary area, which we edited over here, by creating 81
a new NSManagedObject inside that context. 82
And then we save the context so that we can commit unsaved changes to our PersistentStore. 83
So I know there's quite a lot of new words and new concepts that are being presented in this lesson, 84
so it might be worth watching this lesson a couple of times, 85
and also to finish watching the Core Data module, and coming back to review some of these concepts 86
once you've done it yourself. 87
So at this stage of the course, you're going to start to see a lot more new concepts coming at you 88
and the learning curve is going to ramp up. 89
So you might find that a lot of the knowledge here needs some time for it to soak in or some repeated practice 90
for it to really make sense. In the next lesson, 91
we're going to look at how we can address the R in CRUD or rather reading items from our persistent 92
container. 93
So for all of that and more, I'll see you on the next lesson.