Core Data Overview

Sayali Kale
4 min readOct 15, 2018

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems.

Data Storage and Management for iOS and OS X

If you're reading this article then I assume that you are new to Core Data, You may have heard about it and you are curious about what exactly it is and in which situations one can use it.We won’t be focusing on any code in this tutorial. Instead, I want to focus on the terms that are used in Core Data . If this description fits you, then take a seat.

Without knowing the basics of core data its very hard to understand the ins and outs of core data.

What Is Core Data

Core Data is often considered as a database. Let me clear this for you “it’s not”. It is the “M” in your MVC(Model View Controller). It is a framework that you use to manage the object graph and persist that object graph.Core Data is not a relational database. It is actually a framework that lets developers store (or retrieve) data from a database in an object-oriented way. With Core Data, you can easily map the objects in your apps to the table records in the database without even knowing any SQL.

When to Use Core Data

The framework is a perfect fit for a large range of applications, but not every application should use Core Data. Applications that need a lightweight model layer shouldn’t use Core Data. Core Data is an excellent choice if you want a solution that manages the model layer of your application.

Core Data VS SQLite

The most important difference between Core Data and SQLite is that SQLite is a database while Core Data is not. That is the most important difference because there is very little to compare. Core Data and SQLite are solutions to different problems.Core Data can use SQLite as its persistent store, but the framework itself is not a database.

Core Data Stack

Core Data stack is a collection of framework object. Core Data stack is a mediator between objects and external data store. Core Data stack handles all the interactions with the external data store. Hence, a programmer can just focus on the business logic.

Stack consist of 3 primary objects.

1. Manage Object Model

2. Persistent Store Coordinator

3. Manage Object Context

1. Manage Object Model

The managed object model, an instance of the NSManagedObjectModel class, loads the data model and exposes it to the Core Data stack.

When the Core Data stack of the application is set up, the managed object model loads the data model from the application bundle.

When the stack is created , NSManagedObject is loaded into Stack.

2. Persistent Store Coordinator

Persistent Store Coordinator sits in the middle of the core data stack. Persistent store coordinator is the heart of the stack. It is used to release the instance of entity that are present in a model.

It creates new instances of entity as well as retrieves the existing instance from persistent store.

NSManagedObject defines the data and NSPersistentStoreCoordinator releases the object from the data in persistent store and passes this data object to NSManagedObjectContext

NSPersistentStoreCoordinator also checks if the data is in a state that matches the definitions of NSManagedObjectModel.It keeps a reference to the managed object model and the managed object context.

3. Manage Object Context

NSManagedObjectContext is the object that your application interacts the most with.

Consider NSManagedObjectContext as a scratch board. If we want fetch the object from persistent store, you bring a temporary copy of that object in to the scratch board. You can make changes in the object however you like. The change in this object and their relationship do not directly reflect in persistent store. The changes reflect in the persistent store only after you “Save Changes”.

NSManagedObjectContext does object validation as well.

Some advantages of using Core Data

  • It is faster than SQLite because it is heavily optimized with regard to caching, lazy loading, and memory management
  • One of the biggest benefit gained by core data is the lazy loading of data and faulting to save on memory
  • It always maintains forward and backward links consistent when objects are added/removed from a relationship

Core Data Limitations

  • There are no logical limitation to Core Data itself beyond those imposed by situational memory, disk space etc.
  • If you want to delete thousands of records, Core Data first needs to load each record into the memory. This might in-turn result in performance issue.

What Next?

Well, it isn’t enough to know only the terms that are used in Core Data. There are many complex things we can do with core data like save, retrieve and modify data, delete, track data changes, add Predicates and complex relationships databases. In my next article I will explain the programatic approach of Core Data with examples. Since you have a fair idea of the terms used, the programatic approach to Core Data would be a little simpler to understand.

Always remember that Core Data stack consists of three objects and each of these objects have a specific role to play.

Have Fun with Core Data!

--

--

Sayali Kale

iOS Developer & Free Time Blogger. My primary focus and inspiration for my studies is Mobile Development.