Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  • Overview
  • Transcript

2.2 Core Data Models

Hi and welcome back to Get Started with Core Data. In this lesson we will create our first data models and have a look at the editor. To start off, we have to create a new data model file. After choosing the name and the location the file system, we are presented with the Core Data Editor. You can create entities, start fetch requests, and different configurations, the last one is important when you have multiple persistent stores rather than a single coordinator. Let's create our first entity, I'm going to call it Quiz. A quiz in our application has two attributes, first a name, which is going to be a string. And a date which is going to be a date object. As you can see, you can choose between a few different types that define the store type in the database, I'm going to talk more about this in a later lesson. Each attribute can have a few settings, which you can see on the right side of the screen. For instance the date is currently optional, which is what we want. The name is a required attribute, so we are going to uncheck this box. I could also set default value or constrain it to a given length range as well as a regular expression datum. There is also a different view of the editor which is more like an entity relationship diagram. You can do all the actions you did before here as well. I'm going to add a new entity called Question. Each question has a question field which I can change to a string in the properties inspector and make it non-optional, as well as an answer with the same attributes. Another important concept is relationships, without them each entity would be isolated and have no connections to each other. A question belongs to a quiz, so I'm going to name it this way I can set the destination entity on the right side and set rules and the type. In our case the defaults are fine but a question can't exist without a quiz, so it isn't an option. The inverse relationship is going to be question, when I select the destination and the inverse, here relationship the quiz and the question will get updated as well. This time it is a too many relationship which gives us a bunch more options. We want our relationship to be ordered but we don't specify a minimum or maximum number of related entities. When a quiz gets deleted, we want all questions to be deleted as well, so we are setting the delete rule to cascade. A normal view of the editor shows everything we changed as well. In my opinion, it is much cleaner this way but if you need to see the connections between entities, the diagram view can be useful. When we are finished, we need to create the managed object subclasses for our entities, we will use those in our application. In the wizard, you need to choose the model and the language as well as the location. xCode will create two files per model, one that contains the class and the second one that's contains an extension with the Core Data properties. Whenever you regenerate the subclasses, xCode will regenerate the extension file, it will leave the main class alone so it's safe to add functionality there. The id and is managed keyword means that the compiler won't complain about certain things like implementation because you tell it that Core Data will take care of it. I have shown you the most basic functionality of the editor, you can do much more with Core Data though. For instance, you could declare an entity as abstract and inherit another from it. An example would be different kinds of questions like multiple choice, free text, etc. In the next lesson, we will hook up our quiz table view controller to use the items fetched from Core Data, see you there.

Back to the top