Core Data

Write and read a few records.

Source code in CoreData.zip

  1. main.m
  2. Class CoreDataAppDelegate
  3. Class Person
  4. CoreData.xcdatamodel

Create the project

File → New Project…
iOS Application
Window-based application
✓ Use Core Data for Storage
Choose…
Save As: CoreData
Save

In CoreData-Info.plist in the Resources folder, remove the “Main nib file base name” line. In main.m, fourth argument of UIApplicationMain is @"CoreDataAppDelegate".

In Resources, double-click on CoreData.xcdatamodeld. The double-click on the CoreData.xcdatamodel underneath it.

An entity is a table in a database. We will create an entity named Person with three properties: lastname, firstname, ss (social security number). Press the plus sign in the lower left corner of the Entity pane. A social security number is nine decimal digits, so its type will have to be Integer 32. Even though the name of the entity is Person, the class of the entity is —for the time being.
File → Save

Create a subclass of NSManagedObject named Person. Highlight Person in the Entity pane.
File → New File…
Managed Object Class
Next
Next
Finish
You have just created class Person in Person.h and Person.m. You can drag these two files into the Classes folder. Note that the class of the entity Person is now class Person. CoreDataAppDelegate.m must #import Person.h. Add the method description to class Person.

Where is the data actually stored?

The following statement in the application:didFinishLaunchingWithOptions: method of the application delegate prints the name of the directory that holds the database file.

	NSLog(@"applicationDocumentsDirectory == %@", [self applicationDocumentsDirectory]);
[Session started at 2011-05-09 09:55:06 -0400.]
2011-05-09 09:55:08.072 CoreData[53011:207] applicationDocumentsDirectory == /Users/myname/Library/Application Support/iPhone Simulator/4.1/Applications/383674CB-A078-472D-8BF4-58061015F783/Documents

Type the following commands into a Terminal window on your Mac. The single quotes are needed because the pathname has a blank.

cd '/Users/myname/Library/Application Support/iPhone Simulator/4.1/Applications/383674CB-A078-472D-8BF4-58061015F783/Documents'
pwd
ls -l
-rw-r--r--  1 myname  staff  20480 May  9 09:55 CoreData.sqlite
sqlite3 CoreData.sqlite .dump
BEGIN TRANSACTION;
CREATE TABLE ZPERSON ( Z_PK INTEGER PRIMARY KEY, Z_ENT INTEGER, Z_OPT INTEGER, ZSS INTEGER, ZFIRSTNAME VARCHAR, ZLASTNAME VARCHAR );
INSERT INTO "ZPERSON" VALUES(2,1,1,987654321,'Jane','Doe');
CREATE TABLE Z_PRIMARYKEY (Z_ENT INTEGER PRIMARY KEY, Z_NAME VARCHAR, Z_SUPER INTEGER, Z_MAX INTEGER);
INSERT INTO "Z_PRIMARYKEY" VALUES(1,'Person',0,2);
CREATE TABLE Z_METADATA (Z_VERSION INTEGER PRIMARY KEY, Z_UUID VARCHAR(255), Z_PLIST BLOB);
INSERT INTO "Z_METADATA" VALUES(1,'CBB2F3DD-6932-4575-978F-CAFE022BC0F2',X'62706C6973743030D60102030405060708090C0D0E5F101E4E5353746F72654D6F64656C56657273696F6E4964656E746966696572735F101D4E5350657273697374656E63654672616D65776F726B56657273696F6E5F10194E5353746F72654D6F64656C56657273696F6E4861736865735B4E5353746F7265547970655F10204E5353746F72654D6F64656C56657273696F6E48617368657356657273696F6E5F10125F4E534175746F56616375756D4C6576656CA0110140D10A0B56506572736F6E4F10206C7C8A97F0962F3F7D3898BDBFADFCA426E2540575B676748DC7F1F71965468F5653514C6974651003513208153656727EA1B6B7BABDC4E7EEF00000000000000101000000000000000F000000000000000000000000000000F2');
COMMIT;

Dump the table as comma-separated values.

sqlite3 CoreData.sqlite
sqlite> .mode csv
sqlite> select ZLASTNAME, ZFIRSTNAME, ZSS from ZPERSON;
Doe,Jane,987654321
sqlite> .exit