Java Perspective: Core Data and the Mac OS X Developer
With the introduction of OS X 10.4 Tiger came a host of new frameworks for developers to play with. Chief among these frameworks is Core Data, which is akin to Hibernate except that it does not require any coding nor does it require any configuration files. Naturally, the program itself will require code to access the Core Data portion, but it represents a tremendous reduction in code.
In addition to handling the mess of talking to a persistent store, Core Data also handles the persistent store itself. All saving and loading of data is handled directly by the framework and leaves the developer free to focus on the business logic of the application. Finally, Core Data also handles the creation of the data objects themselves. Instead of having to create potentially hundreds of objects that are little more than structs, the developer simply draws the object model and calls it a day.
Building an Example
To emphasize the usefulness of this new framework, I will walk through a simple example of its functionality. It honestly took longer to write up the explanation of how the application works then it did to write the application itself! The time saving factor inherent in Cocoa development, coupled with Core Data, is simply amazing.
This example application is designed to keep track of hire dates and pay rates for a list of people. The single window for the application will include a table listing all the people being tracked and fields below the table to be able to update the information. The first step is to create the XCode application. I named the application CD1, but you are naturally free to name it anything you choose.
In the New Project Wizard, I selected Core Data Application and created the project. In the main project window, there is an additional group called Models. In this group is the default data model for this application. Although it is possible to create multiple models inside of one application, the default is more that sufficient for this example (see the following figure).
Opening the data model opens up a design window that allows us to edit the model directly, as shown in the following figure.
This is a Core Data object model, and the objects designed in this model map directly as objects for the application to use. In this example, we have only one object to work with: the Person object. Inside the Person object are four attributes: firstName, lastName, hireDate, and payRate. firstName and lastName are both defined as Strings. hireDate is defined as a Date, and payRate is defined as a Double. After the object is complete, save the model and close the window.
The next step is to build the GUI for this application. Under the Resources group in XCode, open the MainMenu.nib file. Because this is a single window application, only the MainMenu.nib file is needed. Update the window to match the following figure.
Now that the GUI is complete, it is time to hook everything up. Because we'll be dealing with an array of Person objects, we'll want an NSArrayController. Select the Cocoa-Controllers Palette and drag an NSArrayController onto the nib. Rename this controller to Person. Next we need to specify what this NSArrayController will be holding:
- Open up the inspector while the Person NSArrayController is selected.
- Go to the Attributes tab.
- Change the Mode from Class to Entity. The result should look like the following figure.
To complete the NSArrayController, we need to connect it up to a ManagedObjectContext. The context is responsible for managing all the Core Data objects. When a new Core Data project is created, code is added to the application delegate that creates a default managed object context. For this example, that default context will be perfect. In the bindings tab for the Person NSArrayController, follow these steps:
- Open up the ManagedObjectContext section.
- Change the Bind To to the Application Delegate.
- Set the Model Key Path to managedObjectContext. The end result is shown in the following figure.
Everything else in the GUI will link to the NSArrayController. The next step is to connect the buttons. Control-drag from the three buttons on the main window to the Person NSArrayController. Connect them to the Add: Remove: and Fetch: targets. Next we need to connect up the table:
- Double-click on the first column (First Name). You might need to click a few times to get through the scroll view and the table view to get the actual column selected.
- Open the bindings tab for the inspector of this column.
- Open the value section.
- Change Bind To to the Person NSArrayController.
- Ensure that the Controller Key is set to arrangedObjects.
- Set the Model Key Path to firstName. The result should resemble the following figure.
Repeat this process for each column in the table changing the Model Key Path as appropriate.
Next we need to connect the NSTextFields to the NSArrayController:
- Select the NSTextField that will be associated with the firstName attribute.
- Go to the bind tab in the Inspector window.
- Open the value section.
- Change Bind To to the Person NSArrayController.
- Ensure that the Controller Key is set to selection.
- Set the Model Key Path to firstName. The result should resemble the following figure.
Repeat this for each field in the GUI, changing the Model Key Path as appropriate.
After this is complete, save the nib file and close Interface Builder. Back in XCode, build and run the application. Yes, that's it! We are done, and the application is complete. Testing the application, you can click Add to create a new Person object, enter data into the fields, and then all the changes are saved automatically. You can even close and reopen the application and the data will persist.