Mastering In CoreData (Part 4 CRUD in Core Data Object Oriented Style)
In order to demonstrate basics of the Core Data using Object Oriented way, let’s use our previous project and make it to perform CRUD operation in an object oriented style for that we need to subclass NSManagedObject.
Follow these steps to create Core Data managed object subclasses with Xcode:
- Select your Core Data model in the project navigator.Xcode displays all your entities in the outline view of the editor area.
- Select an entity in the outline view, and verify that the Codegen pop-up menu is set to Manual/None in the Data Model inspector as shown in Figure 1. Repeat this step for all entities you want to create Core Data managed object subclasses. Since we only have one Entity Task we performed this task on Task entity.
Choose Editor > Create NSManagedObject Subclass… as shown in Figure 2
In the sheet that appears, select the data model that contain your entities, and click Next. Figure 3 Select your data model from the list.
- In the sheet that appears, select the entities whose Core Data managed object subclasses you want to create, and click Next. Figure 4 Select your entities from the list.
In the sheet that appears, select a location to save your files, and click Create as shown in Figure 5
Xcode creates and saves files named ClassName+CoreDataClass and ClassName+CoreDataProperties for each of your selected entity in the selected location where ClassName is the name of your entity’s
NSManagedObject subclass. ClassName+CoreDataClass implements the
NSManagedObject subclass as shown in Figure 6.
ClassName+CoreDataProperties implements a ClassName+CoreDataClass extension (for Swift apps) or category (for Objective-C apps) as shown in Figure 7.
Create Record to Core Data In Object Oriented Style
Fetch Record from Core Data In Object Oriented Style
Delete Record from Core Data In Object Oriented Style
We just finished CRUD operations in an object-style way. As we said in part1.
A Core Data stores thing in a way that looks very object oriented to our code. Entity refer to Class, attributes refereed to var and relationship refer to var pointing to other class
In the next part we will be talking about the relationship between Entities.