CoreData

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object lifecycle and object graph management, including persistence.

Core Data typically decreases by 50 to 70 percent the amount of code you write to support the model layer. This is primarily due to the following built-in features that you do not have to implement, test, or optimize:

  • Change tracking and built-in management of undo and redo beyond basic text editing.
  • Maintenance of change propagation, including maintaining the consistency of relationships among objects.
  • Lazy loading of objects, partially materialized futures (faulting), and copy-on-write data sharing to reduce overhead.
  • Automatic validation of property values. Managed objects extend the standard key-value coding validation methods to ensure that individual values lie within acceptable ranges so that combinations of values make sense.
  • Schema migration tools that simplify schema changes and allow you to perform efficient in-place schema migration.
  • Optional integration with the application’s controller layer to support user interface synchronization.
  • Grouping, filtering, and organizing data in memory and in the user interface.
  • Automatic support for storing objects in external data repositories.
  • Sophisticated query compilation. Instead of writing SQL, you can create complex queries by associating an NSPredicate object with a fetch request.
  • Version tracking and optimistic locking to support automatic multi-writer conflict resolution.
  • Effective integration with the macOS and iOS toolchains.

Creating an Entity and Its Properties

When you start a new project in Xcode and open the template selection dialog, select the Use Core Data checkbox. A source file for the Core Data model is created as part of the template. That source file will have the extension .xcdatamodeld. Select that file in the navigator area to display the Core Data model editor.

To create an entity

  1. Click Add Entity.
  2. A new untitled entity appears in the Entities list in the navigator area.
  3. Select the new untitled entity.
  4. In the Entity pane of the Data Model inspector, enter the name of the entity, and press Return.

To create attributes and relationships for the entity

  1. With the new entity selected, click the Add button (+) at the bottom of the appropriate section.
  2. A new untitled attribute or relationship (generically referred to as a property) is added in the Attributes or Relationships section of the editor area.
  3. Select the new untitled property.
  4. The property settings are displayed in the Relationship pane or Attribute pane of the Data Model inspector.
  5. Give the property a name, and press Return.
  6. The attribute or relationship information appears in the editor area.

Employee entity in the Xcode Data Model editor shows an entity called Employee, with attributes that describe the employee: date of birth, name, and start date.

At this point, you have created an entity in the model, but you have not created any data. Data is created later when you launch your application. These entities will be used in your application as the basis for the creation of managed objects (NSManagedObject instances). A managed object model allows Core Data to map from records in a persistent store to managed objects that you use in your application. The model is a collection of entity description objects (instances of NSEntityDescription). An entity description describes an entity (which you can think of as a table in a database) in terms of its name, the name of the class used to represent the entity in your application, and what properties (attributes and relationships) it has.

Defining an Entity

Now that you have named your entity, you define it further in the Entity pane of the Data Model inspector; see Entity pane in the Data Model inspector.

 

Abstract Entities

Specify that an entity is abstract if you will not create any instances of that entity. You typically make an entity abstract if you have a number of entities that all represent specializations of (inherit from) a common entity that should not itself be instantiated. For example, in the Employee entity, you could define Person as an abstract entity and specify that only concrete subentities (Employee and Customer) can be instantiated. By marking an entity as abstract in the Entity pane of the Data Model inspector, you are informing Core Data that it will never be instantiated directly.

Entity Name and Class Name

Note that the entity name and the class name (a subclass of NSManagedObject) are not the same. The entity structure in the data model does not need to match the class hierarchy. Above shows a class name with the recommended class name pattern of Objective-C, along with a, MO suffix. An entity name and a class name are required.


Author: Vineesha Vasamsetti – Sr. iOS Developer
Source: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CoreData/KeyConcepts.html#//apple_ref/doc/uid/TP40001075-CH30-SW1
2018-10-17T16:09:02+00:00