What Are Core Data Entities And Attributes
It is very common for people new to Core Data to be overwhelmed by the framework's terminology. Core Data is a framework you cannot start working with without a basic understanding of what makes the framework tick.
Even though Core Data is capable of persisting data, it is not a database and you should not treat it as such. That said, Core Data has a number of things in common with databases and that is what confuses many developers new to the framework.
In this tutorial, I would like to zoom in on entities, attributes, and relationships. What is an entity? How does an entity relate to attributes and relationships? At the end of this tutorial, you should have a good understanding of these fundamental Core Data concepts.
What Are Core Data Entities?
Even though I emphasized that Core Data is not a database, a Core Data entity is very similar to a table in a database. But an entity is more than that. A Core Data entity encapsulates quite a bit of information. Let me show you what that information looks like.
Open Xcode and create a new project based on the Single View Application template in the iOS > Application section.
Name the project CoreData, set Language to Swift, and check Use Core Data at the bottom. By checking this checkbox, Xcode automatically adds a data model to the project.
In the Project Navigator on the left, select CoreData.xcdatamodeld to open the data model in Xcode's data model editor. You can add an entity to the data model by clicking the Add Entity button at the bottom of the data model editor. Doing so adds a row to the Entities table at the top.
Select the entity, open the Attributes Inspector on the right, and change the entity's name to Note. The Attributes Inspector shows us that an entity is much more than a table in a database.
An entity is represented by an instance of the
NSEntityDescription class. This class provides access to a wide range of properties, such as its name, the data model it is defined in, and the name of the class the entity is represented by. In short, the
NSEntityDescription class gives you access to every bit of information Core Data has about an entity. This brings us to properties.
What Are Core Data Properties?
Before we discuss properties, I would like to make clear what the difference is between properties, attributes, and relationships in the context of Core Data. The difference is straightforward. Attributes and relationships are both properties. The term property is used to refer to both attributes and relationships. Let us now take a look at attributes and relationships in more detail.
Attributes and relationships are both properties.
You can think of attributes as the columns of a table in a database. Attributes store the values of a Core Data record. There are several types of attributes, such as String, Date, Integer, Float, and Boolean.
Select the Note entity in the data model editor and click the + button at the bottom of the Attributes table. With the attribute selected, open the Attributes Inspector on the right and set Name to title and Attribute Type to String.
Entity names should always be upper camel case whereas attribute and relationship names should be lower camel case. It is important to stick to this convention. Every attribute type has different configuration options in the Attributes Inspector. The String attribute type, for example, allows you to set a default value and to add validation rules. It goes without saying that attributes are much more than the columns of a table in a database.
Relationships are also properties. Instead of storing values, they store a reference to another (or the same) Core Data record. Before we can add a relationship, we need to add another entity. Add a new entity to the data model and name it Attachment. Add an attribute, name, and set Attribute Type to String.
Select the Note entity and click the + button at the bottom of the Relationships table to add a relationship. Select the relationship and set Name to attachment in the Attributes Inspector.
A relationship needs to have a destination. In needs to point to another entity. Set Destination to the Attachment entity. The vast majority of relationships also have an inverse relationship. Let me show you what that means.
Select the Attachment entity, add a new relationship, and set its name to note. In the Attributes Inspector, set Destination to Note and Inverse to the attachment relationship of the Note entity. The inverse relationship is the one we added a few moments ago to the Note entity.
If you select the Note entity, you can see that the inverse relationship of the attachment relationship is automatically populated with the note relationship of the Attachment entity. Xcode is smart enough to figure this out on its own.
Another nice aspect of Xcode's built-in support for Core Data is the graph style of the data model editor. Switch to the graph style by clicking the segmented control at the bottom of the data model editor.
The data model graph displays an overview of the entities, attributes, and relationships we have added to the data model. This visual representation is especially useful for visualizing relationships.
In a Nutshell
Entities, attributes, and relationships are fundamental concepts you need to understand to be productive with Core Data. Questions? Leave them in the comments below or reach out to me on Twitter.