Creating the Core Data model files

In this recipe we will learn how to create the associated Core Data model file object definitions for our BooksLibrary database schema.

Getting ready

Following on from our previous recipe, ensure that our BooksLibrary project file is open.

How to do it...

Before our application can start to use our BooksLibrary database, we need to create the entity class definitions that will define the variables the database store contains so that we can access these through code.

  1. Select the BooksLibrary.xcdatamodel file from Project Navigator window.
  2. Choose File | New | File… or press Command + N.
  3. Next, choose Core Data from within the iOS group.
  4. Then, select the NSManagedObject subclass from the list of available templates.
    How to do it...
  5. Click on the Next button to proceed with the next step within the wizard.
  6. Click on the Create button to save the file to the folder location specified.
  7. Next, we need to define the entities for which we want to create the NSManagedObject classes for.
    How to do it...
  8. Select the Books entity from the Select the entities you would like to manage list and then click on the Next button to proceed with the next step in the wizard.
  9. Ensure that the Use scalar properties for primitive data types option has not been ticked, then click on the Create button to generate the NSManagedObject class files.

How it works...

In this recipe, we learned about the NSManagedObject class, and how we can use this to define the class for the Books entity that we created in the Core Data store, as well as defining the table schema fields so that when we want to use the Books class, we can access the attributes at runtime.

The Core Data model wizard generated two files for us. The Books.h interface file and a Books.m implementation file. The Books.h interface file contains each of our entity attribute fields with each being declared based on their object types, and the Books.m implementation file contains each of our entity attribute fields with each being declared as dynamic. This defines the entity attribute properties so that they can be used when data is being written or retrieved from the Core Data Model

Note

For more information about the dynamic data type, you can refer to the Apple Developer documentation at the following URL: https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html%23//apple_ref/doc/uid/TP40002154-SW9

There's more…

One thing to keep in mind when working with Core Data is that if you try to add a new field to the data-model schema, your application will crash. You will need to regenerate the NSManagedObject files, and then reset the simulator or delete the application from the iOS device.

See also

  • The Adding and configuring the storyboard recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset