Core Data is an object-graph management and persistence framework. Core Data is not a relational database or a relational database management
I did not internalize the meaning of these sentences until I was looking at some
JSON, and then I had an “ah-ha!” moment: JSON is an object graph and can be
described in code and therefore tracked with Core Data.
JSON is an object graph
An object graph is a series of related objects. That’s it. JSON is a statically
defined object graph and clearly shows how objects relate to one another.
Below is an example object graph for an app which I’m building for NOAA
Fisheries. The root object represents a single vessel trip and each trip has
children which represent a series of catches. There is a 1:n relationship
between trip and catches. This is represented in JSON thusly:
There are 2 distinct objects here. We’ll name them Trip and Catch. Knowing
this, we can translate this JSON into Objective-C.
In order to use Core Data, you must subclass NSManagedObject for all the
objects you wish to track. You ought to read the official documentation on Core
Data for more information.
The managed Trip object
The managed Catch object
The code below creates the object graph between the Trip and Catch objects.
At the bottom of the routine, you’ll see a series of NSLog() statements. This
shows how to traverse the object graph.
Everything between // BEGIN Core Data boilerplate and
// END Core Data boilerplate sets up the Core Data stack. For the purpose of
this article, you do not need to understand what is happening, but know that it
is required for all Core Data applications.
To understand Core Data, you must read these documents in this order: