Using Core Data: Workshop 1

If you are a new visitor to our site, please read the Getting Started guide.

The focus of the “Using Core Data” workshop series is to show you how to use classes and methods of the Core Data framework in an iOS application to persist and restore its data. I assume you’ve already gone through these workshops: Fundamental Storyboarding and The tableView Workshop. Now, in this workshop you will learn fundamental Core Data terms.

Core Data – First of all Core Data is not a Database, it is an object-oriented framework for persisting and restoring an iOS applications data. You already know that an iOS application is created using the Model-View-Controller architecture. Core Data creates and manages the Model layer of an iOS application. Take a look at this diagram, it is a visual of how Core Data communicate with an iOS application’s Controller and the SQL engine.


Core Data Stack – It is the term used to collectively reference three main classes of the Core Data framework: NSManagedObjectContext, NSManagedObjectModel, and NSPersistenceStoreCoordinator.

NSManageObjectContext – In a Core Data application, a manage object context (context for short) is the workhorse of the Core Data application. Of the three components of the Core Data stack, you will use the context the most. The context holds managed objects and their relationship to one another. The context holds a history of each managed objects so you can provide undo and redo functionality in the application. The context use a Persistence Store Coordinator to communicate with the Persistent Store.

NSManagedObjectModel – It is a class used for referencing your application’s data model. You can think of a managed object model as a database table. You create one or more models of your application’s data by creating a data model file in Xcode. The file has the extension: xcdatamodeld. You edit the data model file via Xcode’s Data Model Editor. Xcode calls a Managed Object Model an entity. At this point you should remember this: An entity and a managed object model reference the same thing-the application’s data model. This image shows three entities created in a data model file.


NSPersistenceStoreCoordinator – It is responsible for managing the context and coordinating access to the persistent store (SQLite database file).

This diagram shows components of the Core Data stack. You set it up in your iOS application’s AppDelegate class.


Persistent Store – The medium you use to store the application’s data is known as a persistent store in Core Data. As you can see in the image above, Core Data offer three types of Persistent Store.

Managed Object – A single row in a database table is known as a managed object. Each managed object is like a NSDictionary. A managed object’s key is the table column name and its value is the column’s data. Because a managed object is stored as a key-value pair in the persistent store, you can subclass the managed object; thus making it easier to work with managed objects (data) in your Core Data application.

EntityDescription – A database table in Core Data is known as an EntityDescription.

Attributes – Columns in a table are known as Attributes in Core Data.

Predicate – It is a conditional expression that evaluate to true or false. In the Structured Query Language (SQL for short), a predicate is used in a statement containing a WHERE clause to restrict the row that is retrieved from the database’s table. The condition you provide after the WHERE clause filters the rows retrieved from the database’s table and gives you only those rows which you expected to see. The WHERE clause can be used in a sql SELECT, DELETE, or UPDATE statement.

In Core Data, a predicate is used in a fetch request to restrict the managed objects (rows) that’s retrieved from the persistent store (database file). The fetch request’s condition is provided as an argument in the predicate’s predicateWithFormat: method. Think of the predicateWithFormat: method as the WHERE clause of a sql SELECT statement. So, in a nutshell, a Core Data predicate is equivalent to a sql SELECT statement, which fetch managed objects (rows) from the persistent store (database) that satisfy a condition that is evaluated to true.

  • Thank you for sharing this information. I am an Android developer and and now doing some iOS work. I wanted to know, how to use predicate for updating data in Core Data, something like UPDATE `table_shops` SET `distance` = ‘10.00’ WHERE `shop_id` = 10. I understand that predicate is used for SELECT, but I have not been able to find an elegant way to update records using predicate or something similar.