Elementary Introduction to CoreData: Using Xcode 4.3.2 and iOS 5

Part of the iOS Category.

Update 09/03/2012: I added a new Core Data and JSON Tutorial with source code.

Update 04/17/2012: I thank Gabriel Massana who contacted me to correct the errant labeling and related images in steps 29 – 32 in my original post.

On March 26, 2012 I gave the following Xcode and CoreData demos presentation to the SDiOS group. There were 27 people present.

Many new programmers want to understand how to store data in an iOS device. There are a number of ways to do that. In some cases CoreData is the approach to consider—and use. My presentation/tutorial introduces the basics of CoreData in the first example.

It important to understand that CoreData is not a database itself, it is an object persistent technology from Apple. The slideshow pdf covers this point in more detail.

A second more practical iOS demo implementation, CoreDataBooks available from the Apple iOS Developer website. That CoreDataBooks demo is discussed in the KeyNote Slideshow PDF link described further down.


The basis of one of the demos is from two YouTube videos by Madhu Kishore Kalva and may be referenced below as a comparison to his work:

I used Keynote ’09 for my slides and then jumped into the demos proper. It was a evening of much discussion about CoreData, other backend data storage mechanisms, and other side topics.  Technical presentations often encourage side tracks in discussions as part of the experience. 😉

This CoreData demos shown used:

  1. a MacBookPro running 10.7.3
  2. Xcode 4.3.2
  3. iOS 5
  4. SQLite Database Browser
  5. Safari 5.1.4
  6. An Internet connection

The slideshow discusses how to obtain Xcode and CoreData.

In addition to the other informational parts of this demo, I display the reduced-size image versions below that can be used as sequential steps for creating your own version of the first demo or to just follow along with the Xcode project file indicated further below.

A zipped folder of the full-size images can be downloaded for better readability. The numbered steps below correlate with the prefix numbers of the filenames on those larger set of images.

The second zipped folder contains the actual Xcode project files and presentation in pdf format. The slides are in pdf format so you do not need Keynote ’09 to view them. Embedded in the pdf are links (in green and yellow) to slides also to the second Xcode project demo and additional resources.

Download the zipped folders file to begin. Then follow the ReadMe document located in the SDiOSDemo Project Folder  to better understand the tutorial demo and begin your own further explorations of CoreData.

This demo presumes you have some previous experience with Xcode and are familiar with how its interface controls generally operate. You may also need to know how to research with the Organizer part of Xcode and across the Internet if you need more understanding of this demo parts.

*** Please review the “ReadMe” document in the Zipped Folder of the SDiOSDemo Project before starting the project.

Step 00: Start Xcode 4.3.2 and select a New Project from the File Menu. Then click “Create a new Xcode project” and Open button to begin.

Step 01: Click the indicated project template, “Single View Application” then click Next.

Step 02: Name the project as shown except don’t use “com.newbound” as the “Company Identifier“, instead use your own. 😉

Step 03: This is the initial project window. Review Xcode documentation for further explanation of parts from Xcode’s “Help Menu“.

From this point forward, you should consider using the downloaded full-size image versions linked above to have more clarity if the displayed images. 🙂

This image reflects the project window after it opens in the first instance. The next few slides help illustrate the process steps to build the application and compare your progress of same.

Step 04: This image shows the expanded Navigator portion of the project window and the orientation choices for the device itself.

Step 05: It is recommended to click the “Validate Settings” button at the bottom of the project window to see that you’re off to a good stable start.

Step 06: This image shows that not all the files in the Navigator area are necessarily reflected in the Finder’s Project Window.

Step 07: This project will not use a .xib file to display the results. The program will use the Console Output to display results.

Step 08: The project template we are using comes with a few included Frameworks. This view shows them.

Step 09: We need to include the CoreData Framework into our project.

Step 10: This image shows how the window looks after successfully including the CoreData Frameworks into the project.

Step 11: I like to keep my necessary files under the “SDiOSDemo” folder in the Navigation area. We’ll move it in a moment.

Step 12: This view indicates the move of the CoreData Frameworks into the “Frameworks” folder.

Step 13: Notice that the CoreData Framework does not appear in the Finder’s Project Folder.

Step 14: Now we need to create our Data Model and we need a new file for that. Start in the File Menu and look in the Project selection to create this, “Data Model” document as shown.

Step 15: We’ll save the Data Model document to the SDiOSDemo Project Folder.

Step 16: When you click on the “Data Model.xcdatamodeld” file in the Navigation area, you’ll see the Data Model Window appear.

Step 17: The “Data Model.xcdatamodeld” file is moved to the “Supplementary Folder” in the Navigation area. We move items around to our liking as a matter of organization.

Step 18: “Data Model.xcdatamodeld” file does appear in the Finder’s Project Window.

Step 19: Create the Author Entity as shown by clicking the “Add Entity” button. Then add the attributes by clicking the “+” button below the Attributes pane of the window. Enter the names as shown and select the datatype by clicking the arrow menu next to the attribute name.

There are a number of online information resources to help explain the design elements and how they are to be used in the Data Model Window. The “Organizer” section of Xcode can supply the information too. It is a good resource for understanding Xcode as well as iOS frameworks.

Step 20: This is how to enter the data for the Publisher Entity in the same way as shown for the Author Entity.

Step 21: And finish up with the BooksPublished Entity likewise.

There are two relationships established for this BooksPublished Entity. create then in a similar fashion to the Entities.

Step 22: This is a graphic view of the Entities that is presented by clicking the “Editor Style” button in the bottom right of the Project Window.

Step 23: This picture shows the “Relationships” between the Entities after they’ve been created. The double arrows indicate a “one-to-many” records relationship. There are a few BooksPublished records connected (in this example) either back to the Author record and a Publisher record. The connections can be created here by Control-Dragging from one Entry to another and setting the “one-to-many” setting in the Inspector pane of the Project Window.

Step 24: This image illustrates how to create the required NSManagedObjects Subclasses. Select the three Entities as shown and then click the “Generate NSManagedObjects Subclass…” from the Editor Menu of Xcode.

Step 25: You will be asked where to save these generated subclass files. Save them also in the SDiOSDemo Project Folder.

Step 26: This image shows the “Author.h” file that Xcode created for you to enable CoreData Frameworks to work with your project. The generated files should not be directly edited or changed in any way. Just leave them alone.

Step 27: The related “Author.m” file

Step 28: In like terms the “BooksPublished.h” file.

Step 29: In like terms the “BooksPublished.m” file.

Step 30: In like terms the “Publisher.h” file.


Step 31: In like terms the “Publisher.m” file.

Step 32: These are the modifications to the “AppDelegate.h” file.

Step 33: When the program runs, the output will be displayed in the Output  Pane of the Debugger section of Xcode.

Step 34: This picture uses the external SQLite Database Browser to connect to the CoreData created database. When this program runs on the Simulator the database will be located on your hard disk at:

/Users/”Your Home Folder Name Here”/Library/Application Support/iPhone Simulator/5.1/Applications/”Some Multi-Character long Folder Name”/Documents/Default SDiOS Database

You’ll need to substitute “Your Home Folder Name Here” with yours and “Some Multi-Character long Folder Name” with what is located at the path:

/Users/Your Home Folder Name Here/Library/Application Support/iPhone Simulator/5.1/Applications/

On my hard disk, the most recent running of the application put the data base at:

/Users/dwlarson/Library/Application Support/iPhone Simulator/5.1/Applications/A1D0D789-0546-44C0-B07B-5BF8793CEF01/Documents/Default SDiOS Database

The “A1D0D789-0546-44C0-B07B-5BF8793CEF01” portion of the path will change each time you create the database.

You’ll need to navigate the SQLite Database Browser to your database to use the review the database structure SQLite created as well as the records this program inserted into the tables that map back to those respective Entities.

What you’re seeing below is the way SQLite maintains the database structure. You’re probably most interested in the down-toggeled arrows of the window and the related Entity Attribute Names prefixed by a “Z“, SQLIte’s naming convention for its parts.

Step 35: Using the “Browse Data” tab, these are the two Author Table records created back in “AppDelegate.m” Class file in the ”

insertDataIntoCoreDataDatabase” method.

Step 36: In like terms the records of the Publisher tables are displayed.

Step 37: In like terms the records of the BooksPublished tables are displayed. There are a combination of five records between the two Author records and two Publisher records.

Step 38: In the included file, “SQLite Query.txt” in the Zipped Folder link referenced near the top of this article there are a few commands you can copy and paste into the “Execute SQL” tab of the SQLite Database Browser application. After pasting in the copied text, click the “Execute Query” button to see the results.

Step 39: The completed Finder’s Project Folder should appear similar to the one below.


Did you like this? Share it:

About Don Larson

Using computer technology since June 1980.
This entry was posted in Education, iOS, iOS Tutorials, Native Apps, Technology. Bookmark the permalink.