5.1 Creating Entities and Relationships
At the very center of Core Data is the concept of Entities and Relationships. Entities are like objects that we can store within Core Data. A Relationship, then, is what you use to tie together different entities.
1.Introduction3 lessons, 06:16
2.Setting Up the Project5 lessons, 1:00:13
3.Processing Data3 lessons, 37:52
4.Interacting With the UI6 lessons, 41:27
5.Core Data6 lessons, 44:36
6.Conclusion1 lesson, 02:47
5.1 Creating Entities and Relationships
Now that we have a basic idea as to what core data is, let's go ahead and actually start to put it to use. Now, if you recall several lessons ago when I was creating the models for this particular application, I made sure to name them FeedModel and ArticleModel. And I referenced the reason that I did that was because we were going to start creating things within core data, and I wanted to make sure that we were differentiating the models between what was gonna happen in core data. Now, the reason that I did that was, when I'm following the MVC pattern, I like to create models for models' sake. Now, to me the model within MVC is encapsulating data and maybe a little bit of functionality and business rules that ultimately are going to somehow be presented to the end user via the view and the view controller. Now what wes, now what we are putting into core data and getting out of them is entities, and these entities are going to contain data that we want to persist via objects that we want to store for later use, or update, or delete, or whatever have you. But what I want to be able to do is store data within my entities that are going to look remarkably similar to my models. But there may be additional data that's going to be around the storage of these data, or of this data that doesn't necessarily need to make it to the end user. So what I wanna do is make sure that my models are made for presenting data or there to present data to the end user, and my entities are used for storing data. Those can be kind of a gray line there sometimes, but that's how I like to separate things. We definitely could have built this entire application around the entities that we're gonna store within core data, and if that's the way you wanna do it, then that's completely up to you. This is just a separation of concern that I like to do within my applications. So, if we head over into the Swift reader data model here. We have a nice little modelling interface here that we can start to create some entities and some relationships between them. So let's go ahead and start by creating an entity. So I'll come down to this bottom button that says Add Entity. Now you can see that it's going to create a new entity under the entity's title here on the left-hand side. So I double-click this. I can change the name to Feed. So I'm gonna create a Feed entity. Now, the next thing that I can do is I can create attributes associated with this entity. And you can think of attributes simply as properties that are gonna be associated with this, with this feed entity object. So, within our model, if you recall, we had three. We have a title, a URL, and a list or an array of articles. So let's do something similar. So I'm gonna create an attribute called title and the type of this is going to be a string. Then we had another one which is the URL, that URL that represents where this feed is coming from, so I'll go ahead and save that. And then we also have a list or an array of articles. So let's go ahead and create this article entity, and then we'll worry about creating that relationship. So the next thing we'll do is we'll come down and click our Add Entity button again. We'll double-click this. We're gonna change this to be articles. So this is gonna be an Article entity. Then we're gonna create some attributes here for it as well. So if the ArticleModel has the title, link, and pub dates, let's do something very similar to that. So we'll create our first attribute, which will be title. And once again, this is going to be a string. Then we'll add another one, which is gonna be a link, which is gonna take us directly to that particular article that's also a string. And then we have the pub date, which you could use as a date but I'm gonna leave as a string here just to keep things very simple, so we'll go ahead and save that. So now we have two distinct entities. We have a feed, and we have an article. Now we want to create that relationship between the two. So I'm gonna select Feed because that's going to be my parent object, just like it is within our application, within the navigational flow of our app. And I'm gonna come down to the Relationship section, and I'm gonna to hit the plus button. Now it's going to create a relationship here that we can name, and I'm gonna call this articles just like it's named within our model. And we're gonna have a destination here. The destination means what is basically the type of this relationship. In this case, it's going to be Article. But as you notice here, it doesn't say multiple articles or things like that. It just says that it's going to be Articles, so we can modify that in just a moment, so I'm gonna go ahead and save it. So now we have the concept of a parent feed having some attributes and having a relationship to an article at this point, and then an article having a link, pub date and title attribute. So if I come back to Feed and I select my article's relationship and look over on the right-hand side, I can see some additional information about it, namely the destination or what the type is, the delete rule, and the type. And these last two things are the ones that we're gonna want to change. So if we look at the Delete Rule and take a look at the options we have here, we have Nullify, Cascade, Deny and No Action. So that means if I were to delete the Feed entity, what do I wanna do with all of the relationships? Or at least in this case, what do I wanna do with this articles relationship? I can do nothing. I can nullify it, which means I am just nulling out that relationship so the objects are still there, but they're not really pointing anywhere, and then cascade and deny it. So cascade is ultimately what we want to use because that means if I delete my feed, any associated relationships, in this case, articles, are going to get deleted as well. And I keep saying multiple. I keep saying articles because I want this to be a one feed to many articles relationship, but right now it's only a one to one. And the reason I know that is because the type over here on the right-hand side under Relationship is set to To One, which means this is a one feed to one article. I want to change that to be To Many. So this means that I can have one feed related to multiple articles, which is ultimately what we're looking for. So I'm gonna go ahead and save all of those changes. Now, what I want to be able to do when I'm creating these entities and then persisting them in and taking them out of our core data framework, I wanna be able to refer to these things as feeds and as articles. And one thing you'll notice if I select one of these entities, it'll say the class sphere is an NSManagedObject. So if you don't do anything from here, you can absolutely refer to these things generically throughout your code as NSManagedObjects and use a few helper methods to set some properties like title and URL and articles. That's all fine and dandy, but to me, it just feels like that's additional code that I have to maintain and a lot more code that I could screw up. So what I like to do at this point, I like to come up to Editor. Select Create NSManagedObject Subclass. I'm gonna select the check mark next to my data model, which is SwiftReader. Select Next. Now I'm gonna select which entities I wanted to create classes for, which are Feed and Article, and then hit Next. Now this is gonna ask me where I wanna save them, so I wanna save them within my SwiftReader folder under the Swift language. And then I want to go ahead and click Create. So now what this has done for me is it's created two classes, Feed and Article, that are sub-classes of NSManagedObject that have these special NSManaged properties that represent all of the things that I have created within this SwiftReader data model right here. Now, this is one of the reasons why I made sure to specify that my model classes are actually named FeedModel and ArticleModel, as well as being able to call these entities something very simple, namely Feed and Article. Now, you can name them something else, FeedEntity, ArticleEntity, whatever you really like, but this is just the process that I like to follow. So now that we have these managed objects, in the next couple lessons, we are going to discuss how we're actually going to get them into core data, get them out, update, and all sorts of nice little tricks like that.