Core Data Tips for iPhone Devs Part 1: Command Line Shortcuts

I recently finished migrating the data management backend for all of my games to Core Data from a custom sqlite implementation. This is part of my commitment to use more Apple APIs where it makes sense, and this one made a lot of sense. While the migration took me a couple of days, the benefits of using Core Data have already paid back that investment in improved performance and better data design. Among other things, my domain objects are now much more granular which allows me to pull only the little bits of data that I need when I need them. Core Data makes managing the relationships between all of these little domain objects dead simple. In other words, I don’t have to write any more join queries!

Core Data is a massive topic, and I have only begun to scratch the surface of its usefulness. Still, I have found myself doing some repetitive tasks both at the command line and in code. Like any self respecting programmer, I hate repetitive tasks, and my Makefile and header file of macros have grown as a result. Since most of the shortcuts I have come up with will probably be useful to others, I am putting them together in a series of blog posts. This is the first of those posts.

The Problem

Core Data allows you to choose from multiple backend storage methods, and, other than having to choose a type, the data store is completely transparent. The recommended store on the iPhone is sqlite, and not only is it fast, but the schema generated by Core Data is fairly readable. Being able to poke around in the sqlite file was an immeasurable help while I was learning Core Data. The biggest problem is finding the sqlite database file. Every time you build and run your app in the simulator, the application directory is renamed with a new GUID which makes it really hard to find:

Can you find your app?

Can you find your app?

The Solution

Thankfully, OS X has the full compliment of unix command line tools. Wrap the proper commands in a Makefile, and you have some instant command line Core Data database management tools. Unix to the rescue! Here are the four make targets that I use the most:

SQLITE_DB_FILE=AppData.sqlite
SIMULATOR_HOME=$(HOME)/Library/Application\ Support/iPhone\ Simulator

dbshell:
      find $(SIMULATOR_HOME) -name "$(SQLITE_DB_FILE)" -exec sqlite3 {} ';'

killdb:
      find $(SIMULATOR_HOME) -name "$(SQLITE_DB_FILE)" -exec rm {} ';'

dbschema:
      find $(SIMULATOR_HOME) -name "$(SQLITE_DB_FILE)" -exec sqlite3 {} .schema ';'

dbdump:
      find $(SIMULATOR_HOME) -name "$(SQLITE_DB_FILE)" -exec sqlite3 {} .dump ';'

The target names should make their use self explanatory, but I’ll summarize them:

dbshell
Open up the sqlite command line shell with the Core Data database selected.
killdb
Delete the database file. This is useful if you’re making big changes to the schema and you would rather delete the whole database and start over rather than migrating the schema.
dbschema
Dumps the CREATE statements required to recreate the schema. This is useful to see how Core Data persists your object graph.
dbdump
Dumps the schema as CREATE statements and all of the data as INSERT statements.

There is nothing magical or difficult about these, but I have found them to be very useful while developing my data model.

5 Responses to “Core Data Tips for iPhone Devs Part 1: Command Line Shortcuts”

  1. WordPress › Error

    There has been a critical error on this website.

    Learn more about troubleshooting WordPress.