Sejan Miah
killingMeSwiftly
Published in
2 min readJun 19, 2017

--

Core Data Refresher Part 1

What is Core Data?

Core Data is an object graph management framework that manages a potentially large graph of object instances to persist data. It stores data in a lasting state so when the app relaunches or you reboot your device the data is there for you access. Core Data is not an object-relational mapper (ORM).

What is the difference between Core Data and SQLLite?

SQLLite is a database that operates on objects stored in disk, whereas, core data operates on objects stores in memory. To implement its graph management, Core Data happens to use SQLLite as a disk store. It could have been implemented using a different relational database.

In order to actually use Core Data, first let’s create a managed object model, which describes the way Core Data represents data on disk. The Data Model is a database schema because Core Data uses a SQLite database as the persistent store.

Furthermore, Core Data used the term manages quite frequently. For example, NSManagedObjectContext, managed is used here in the name of a class and refers to Core Data’s management of the life cycle of Core Data objects.

· Entity — Core Data’s class definition.. The classic example is an Employee or a Company. In a relational database, an entity corresponds to a table.

· Attribute is a piece of information attached to a particular entity. Essentially one of the entity’s properties. In a database, an attribute corresponds to a particular field in a table.

· Relationship- the link between multiple entities.

The entity is the class definition and the manages object is the instance of that class.

Thus, NSManagedObject is the instance of that entity, the single object being stored in Core Data. NSManagedObject can be any entity in your Data Model, taking on any attributes and relationships that have been defined. Furthermore, for your NSManagedObjectto access a specific property you defined in your Data Model, you must use key-value coding (KVC).

KVC Notes:

KVC is a mechanism in Foundation for accessing an object’s properties indirectly using strings. In this case, KVC makes NSMangedObject behave more or less like a dictionary at runtime. Key-value coding is available to all classes inheriting from NSObject, including NSManagedObject. You can’t access properties using KVC on a Swift object that doesn’t descend from NSObject.

Finally in order get data from your persistent store into the managed object context, you have to fetch it.

--

--