Join Simon Allardice for an in-depth discussion in this video Creating entities, part of Core Data for iOS and OS X.
- View Offline
So there will be a data model file in any project if you select Core Data as an option, but it can also be added manually to a project. I'm just going to create a new master detail iOS demo with Core Data, same as I did a moment ago. So, finding that data model file and selecting it will open the Model Editor part of Xcode. There is quite a lot of options here, but the first, the most important building block of any data model is something called an Entity. And an Entity is how you describe the structure of your managed objects.
The same way that you'd write a class in normal Objective-C that defines the structure of regular objects you can then instantiate, and we do continue to do that for regular objects. Well, in Core Data, you'll define an entity that describes the basic structure of managed objects. So there could end up being 1,000 managed objects based on any one entity, and we'll have at least one entity, you may have many. In this example, I have one entity showing up on the left-hand section of the Data Model Editor, this is called Event.
I don't have to keep this. It's just the example from the template. So in the left-hand section, I can select it, and I'll see the details of that entity in this middle section here. And I can see that this entry seems to have one thing, an Attribute called timeStamp and timeStamp has a Type of Date. So this application could have multiple managed objects, all instantiated from this Event entity where each managed object will have its own timestamp. Now down here at the bottom of this section, I can add a new Entity to this, clicking the button that allows me to go up here and name it.
I can also use the Editor menu in Xcode when I'm in the Model Editor. So I can add entity, and I start to see them add up over here in the left-hand section. I've also got this Fetch Requests and Configurations areas, we'll get into these later. We don't need them now, but we will always need Entities. And you'll typically name new entities like classes an uppercase first letter, singular noun, so Event, Book, Author, Employee, Account, Player, Person.
And if you're thinking, well, how am I supposed to know what my entity should be in my app? Well, the question is, what would your classes be? If you weren't using Core Data, these entities would be your normal classes for your model objects. So if you are building a bookstore application, you might have classes for book and publisher and author, you'd have entities for those. If you're making a music application, you'd have entities for album and artist and track. And if you're wondering, why do I have to use this new concept of Entity? Why can't I just deal with regular Objective-C classes and objects? Well, realize that we're doing something different here.
An entity might be at the same conceptual level as a class, but unlike a class, we are not trying to define behavior here. So what we're doing right now is not like, say, a UML class diagram. We are not defining method names, any behavior. An entity is pure data, no behavior, no logic. But you might say, what if I want these managed objects, these things that Core Data is going to save, what if I want them to have custom behaviors? Well, we will get to that. Yes, absolutely, they can have custom behavior, but that is not defined in the Entity. The entity is data.
You're simply describing what Core Data will store, what it will persist, what it will manage. Now if you're coming from a relational database background, you might see that what we seem to be doing here looks a little similar to defining a schema for a database where our entities are like tables and the attributes look like columns in those tables, describing what kind of data those columns can hold. And that is true, there are similarities, but it only goes so far. So don't assume all those skills directly apply here, because if you start worrying about things like primary keys and foreign keys, you're going down the wrong path.
It's simpler than that here. However, entities certainly can be more complex than just the timestamp. So next, we'll take a look at our options for attributes.
- Understanding Core Data and object persistence frameworks
- Creating a Core Data project
- Exploring data modeling
- Creating entities, attributes, and relationships
- Creating managed objects
- Fetching in Core Data
- Implementing undo and redo support
- Creating a Core Data Cocoa app without code
- Responding to validation issues
- Converting store types
- Preloading default data