Core Data Fundamentals of iOS Development

A free video tutorial from Dr. Angela Yu
Developer and Lead Instructor
Rating: 4.7 out of 5Instructor rating
7 courses
2,572,713 students
Core Data Fundamentals

Lecture description

In this lesson, we'll introduce you to the fundamentals of core data and how to use it as a database to store data that is used in your app. This is perhaps the most complex form of location data storage, but it is also the most flexible. We'll compare Core Data against Realm and SQLite. We'll also show you how Core Data works relative to OOP and Databases.

Learn more from the full course

iOS & Swift - The Complete iOS App Development Bootcamp

From Beginner to iOS App Developer with Just One Course! Fully Updated with a Comprehensive Module Dedicated to SwiftUI!

59:37:13 of on-demand video • Updated November 2023

You will create a portfolio of 15 apps to be able apply for junior developer jobs at a technology company
You will learn Xcode, UIKit and SwiftUI, ARKit, CoreML and CoreData.
You will learn by doing, where every lesson is incorporated into a real-world app project.
After the course, you will be able to build any app you want.
Start your own app based business
Become a digital nomad by working as a freelance iOS developer
Master creating Augmented Reality apps using Apple’s new ARKit
Create apps that use Machine Learning using Apple’s new CoreML
Master app design so you'll know how to wireframe, mockup and prototype your app idea
Master app marketing so you can publish your apps and generate downloads
Now, we've come across a lot of new words that actually all describe the same concepts that we're really familiar about. So for example, in the object-oriented programming world, we have something called a class, and that in the Core Data world is known as an entity. And when you think about databases, that's simply just a table that represents your data. And in the other case, when we talk about properties in object-oriented programming, it's actually similar to an attribute in Core Data. And in your database, that would simply just be a field or a particular column of your table. So if we look at one of our tables from our bakery, for example, the buyers' table, the table itself is a class. And if we're using Core Data, then the table would be called an entity. Now, each of these columns that have headings like address, number, who to invoice, or the buyer name, these are field in our database. And if we created the table as a class, we would call them properties associated with the class. But in Core Data, they're known as attributes. So every single row here is a Core Data attribute. So the address would be an attribute to the buyers' entity. Now, if we fill up this table and we give each of these properties a value, then every single row we have in our table would be a brand-new NSManagedObject. So there's a lot of new terms in Core Data. But just remember, when we're talking about entities, we're talking about the class and we're talking about the table. When we're talking about attributes, we're talking about the properties of the class or the columns in the table. And when we're talking about NSManagedObjects, then we're simply talking about every single row in our table that is filled with data. So let's zoom out and have a look at our three tables that we saw in the beginning of this module. We have a Buyers' entity. We have a Products' entity and we have a Orders' entity. Now, if we put all three entities or all of our tables into permanent storage, then that becomes our persistent container. And this container is simply a SQLite database that stores all of our tables and all of the relationships between the tables. Now, when we're writing our app, we can't simply interact with the persistent store directly. We have to go through an intermediary which is known as the context. And this context, as I said before, is kind of like a temporary area where you might create new pieces of data that you want to add to your database or reach data in your database, or specify the things that you want to update in the database, or specify the things that you want to destroy in the database. So this is what's known as CRUD. And when people talk about databases, they tend to use this word quite a lot. But it simply just means all the things that you tend to want to do with a database, right? Create, read, update, destroy. And the important thing to remember is that in Core Data, you do this inside the context. You don't do this directly to the persistent container. And inside this intermediate area, what you can do is you can undo the changes or redo the changes. You can simultaneously add and destroy and update. And only once you've decided that you're happy with what you've done inside the context, you save the context. And the Core Data framework takes care of committing the current state of data inside the context to your persistent stores. So as I said before, this is really similar to the Git example when. When we use Git, we first had to add the things that we wanted to commit to source control to a staging area, and that's when we use the add command. Only when we were happy with what we wanted to commit, did we then use the commit command. So in Core Data, we have a persistent container and our app is not allowed to interact directly with a persistent container. Instead, it has to go through this temporary area which is known as the context. And when you're happy with what's in the context, do you then save the context which basically commits those changes to our permanent container. So let's review our code. So far, we've only implemented the C in CRUD, or the create, inside create-read-update-destroy. But in order to do that, we've actually had to come across all the things that we spoke about earlier on, including the context and the persistentContainer. So the first thing we create is a constant called context and this goes into the AppDelegate and grabs the persistentContainer. And then we grab a reference to the context for that persistentContainer. So if we have a look inside our AppDelegate, we have this lazily loaded persistentContainer, so it only gets loaded up once we actually retrieve it or try to use it. And it's basically a new container that's of the type NSPersistentContainer, and it's created using the structure that we've specified inside our DataModel over here which has one entity called Item, and Item has two attributes: a done and a title. Then we load the PersistentStore and get it ready for use. Now, we access the Persistent Stores' context which is that temporary area if you remember, that our app is going to talk to. Next, when we add a new item to our table view, we create a new object of type Item. And remember, this class gets automatically generated when we create a new entity with that name inside our DataModel. And that class already has access to all the properties that we have specified as attributes: title and done. So we create our newItem and that item is an object of type NSManagedObject. So if you remember we said earlier on, NSManagedObjects are essentially the rows that are inside your table. And every single row will be an individual NSManagedObject. And then we fill up all of its fields, so the title field and the done field. And once we've done that, we save our items. And inside the saveItem's function, we have this method that can throw an error, so it's marked with a try and we try to catch the error if there are any issues. But what it does is that it looks at the context, that temporary area, which we edited over here, by creating a new NSManagedObject inside that context. And then we save the context so that we can commit unsaved changes to our PersistentStore. So I know there's quite a lot of new words and new concepts that are being presented in this lesson, so it might be worth watching this lesson a couple of times, and also to finish watching the Core Data module, and coming back to review some of these concepts once you've done it yourself. So at this stage of the course, you're going to start to see a lot more new concepts coming at you and the learning curve is going to ramp up. So you might find that a lot of the knowledge here needs some time for it to soak in or some repeated practice for it to really make sense. In the next lesson, we're going to look at how we can address the R in CRUD or rather reading items from our persistent container. So for all of that and more, I'll see you on the next lesson.