Mastering In CoreData (Part 4 CRUD in Core Data Object Oriented Style)

Core Data

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:

  1. Select your Core Data model in the project navigator.Xcode displays all your entities in the outline view of the editor area.
  2. 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.
Figure 1

Choose Editor > Create NSManagedObject Subclass… as shown in Figure 2

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.

Figure 3
  1. 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.
Figure 4

In the sheet that appears, select a location to save your files, and click Create as shown in Figure 5

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.

Figure 6

ClassName+CoreDataProperties implements a ClassName+CoreDataClass extension (for Swift apps) or category (for Objective-C apps) as shown in Figure 7.

Figure 7

Create Record to Core Data In Object Oriented Style

Add Task

Fetch Record from Core Data In Object Oriented Style

Delete Record from Core Data In Object Oriented Style

Delete Task in Object Oriented Way

Summary

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

What Next?

In the next part we will be talking about the relationship between Entities.

Useful Links

https://store.raywenderlich.com/products/core-data-by-tutorials

https://www.objc.io/issues/4-core-data/core-data-overview/

https://en.wikipedia.org/wiki/Object-relational_mapping

https://cocoacasts.com/what-are-core-data-entities-and-attributes

https://cocoacasts.com/what-is-the-core-data-stack

https://developer.apple.com/library/archive/documentation/DataManagement/Devpedia-CoreData/coreDataStack.html

https://medium.com/@ankurvekariya/core-data-crud-with-swift-4-2-for-beginners-40efe4e7d1cc

https://www.raywenderlich.com/7569-getting-started-with-core-data-tutorial

https://developer.apple.com/library/archive/qa/qa1952/_index.html

Senior iOS Engineer | HungerStation | Delivery Hero

Senior iOS Engineer | HungerStation | Delivery Hero