Core Data: The Basics (iOS 6)

If you’re building an iOS App chances are you will have data that needs a place to live; either in a database (Core Data), or in some file structure (Archiving). Archiving basically just drops everything into the filesystem which can be really handy for images but what about items with quantity, descriptions, and various other properties. That’s where Core Data comes in. Essentially Core Data is nothing more than a middle man between your app and a SQLite database. Ultimately the best solution will be a combination of both. The goal of this tutorial is to guide you through the portion that has to do with Core Data. This guide will take you through setting up an application that will use Core Data, then show you how to interact with a SQLite database through Core Data. To accomplish this we are going to create an inventory application for a local general store and use that application as a reference for our development needs.

Objectives

  • Setting Up
  • Adding Core Data
  • Creating Entities
  • Interacting With Entities

Setting Up 

Note: This tutorial assumes a decent familiarity with XCode 4 and working in the evirnoment. If you aren’t familiar with the following steps or how to navigate a project file I would recommend working through my other post iOS: Hello World – Let’s Do Math (Xcode 4) before continuing

We to do a bit of baseline set up before we can get into working with Core Data. To do this, open Xcode 4, create a new application with the Empty Application template set and set the following properties:

  • Product Name – CoreDataInventory
  • Class Prefix - CD
  • Devices - Universal (pick anything you want here. I’ll be using the iPhone Simulator for examples)
  • Uncheck Use Core Data
  • Check Use Automatic Reference Counting

Q: Why did we uncheck Core Data?

A: If you’ve followed any of my other posts you’ll know I prefer to avoid auto-generated code as much as possible. This will also give us an opportunity to add Core Data to an existing project.

We’ll come back to incorporating Core Data soon but first let’s add more our a base. Create a new UITableViewController without a Xib and name it CDMasterTableViewController. Now open up your CDMasterTableViewController.m and set the title to Inventory List. Also delete all of the pre-generated code so that you are left with:

Go back to CDAppDelegate.m import your new class and in application:didFinishLaunchingWithOptions, init the table view with the Grouped Table View Style.

Then create a UINavigation controller and set masterTableView as the rootView

Finally set the rootView of the window to tableNav. Your application:didFinishLaunchingWithOptions should now look like:

Run your app and it should look similar to what’s below. 

CDInventory Initial Setup iOS6

Adding Core Data

That set up gives us at least the vary basic foundation to build our UI from. We’ll still need to add a bit more but we’ll switch gears to do our Core Data setup. To begin working with Core Data we need to make sure we have the right tools, these tools live in the Core Data framework. To add the framework, open the project file, under Targets select CoreDataInventory and scroll down to Linked Frameworks and LibrariesCoreDataInventory Adding Frameworks Xcode4Click the plus at the bottom of the section, search for and select CoreData.framework then click AddCoreDataInventory Search Frameworks Xcode4This will drop the framework outside of the Frameworks group in your File Explorer. Just to clean up a bit, drag the framework inside the Frameworks group.

Now that we can interact with Core Data, add a new file and in the iOS Core Data group select Data Model.

CoreDataInventory add data model Xcode4Name it CoreDataInventory and click Create.

A Data Model, as you might expect, is the blueprint for how data is stored in your SQLite Database that Core Data will create and communicate with. If you are familiar with SQL Databases your Data Model in this case is your Database, and Entity is a Table in that Database.

Comments are closed.