I was going to be hardcore. I was going to define all my model classes in Objective-C, and I was going to create my object models, and link them to the interface, and be all proud of my working code. And I was going to ignore that pesky problem of persistence, because that was a different battle for another time.
And then I heard about CoreData. If you don’t know what it is, it’s basically a framework that gives you a way to model the persistent data of your application (the model part of MVC) via a very UML-like graphical notation, and then provides you with a unified and consistent way to access the data.
Don’t get me wrong, BTW. Though the graphical notation is UML-like, it’s not UML. It is strictly focused only on data content (and as such does not allow you to define methods, for example). In point of actual fact, it is based on the entity-attribute-value model and as such is a database schema design modeling tool.
Still, after implementing my own quick data model shown below in about ten minutes, I was pretty pleased with CoreData. Okay, I admit it, myself too. But it’s really CoreData that should get the props.
The tricky part for me came when I tried to link the model to the application. I followed the instructions in the CoreData video tutorial slavishly, with a couple of exceptions:
- I had already created my application, and it wasn’t a CoreData Document-based application. It was a plain old application.
- I had a different plan for the UI.
I wasn’t too concerned about the second problem, since the whole point of MVC is that the UI should be pretty independent of your model. I stupidly didn’t even think about the second until, after trying to link my data to my model, I was greeted by an awkward error: my First Responder (as far as I can tell, the object that’s in charge of the application life cycle) did not respond to the managedObjectContext message. Oops.
The managed object context of an application, as explained here, is the way the application accesses all the goodies defined in the model. Since I had not created my application as a CoreData application, my First Responder was completely unaware of the model.
In order to fix this problem, I cheated. I created a new project, made it a CoreData project, and compared the generated template code with my code. I basically copied over any files that were missing from my project (including the all-important delegate that did respond to managedObjectContext), and tried to link them as in the video. There were still a few differences, due to the fact that IntelliTrack is not document based (in other words, users won’t have a “myShipments.intelliTrack” file that gets saved), but for the most part is was smooth.
As a result, I now have an application that looks like this:
And the best part? The “Add” button works, and the new items get persisted all on their own. All this in an evening’s worth of work. CoreData rocks.


Posted by nomothetis