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