Home > Articles > Programming > Java

Java Perspective: Core Data and the Mac OS X Developer

Marcus Zarra
  • PrintPrint
  • Share ThisShare This
  • DiscussDiscuss
Close WindowMarcus Zarra

Marcus Zarra

Learn more…

Basic Animations with Core Animation
Dec 28, 2009
Marcus Zarra and Matt Long on Core Animation
Jun 5, 2009
Introduction to Migration in Apple's Core Data
Mar 7, 2008
A Look at Apple's Core Animation
Feb 1, 2008
The State of Java Development on OS X
Jan 4, 2008
Packaging Your Application for OS X
Nov 21, 2007
Tiger vs. Leopard: Which Should You Develop For?
Oct 26, 2007
Building on Apple Sync Services
Sep 8, 2006
Getting in Sync with OS X
Sep 1, 2006
Syncing Your Data the Cocoa Way
Aug 11, 2006
Controlling Both Ends of the Communications Channel: From Cocoa to Servlets
Jun 2, 2006
Cocoa: Working with XML
May 19, 2006
Java EJB 3.0: A Hibernate Clone?
Apr 28, 2006
Document Level Parameters Using Core Data: A Guide for Cocoa Developers
Apr 7, 2006
Tips for J2EE Development
Feb 24, 2006
The Cross-Platform Java GUI: Designing Code for More Than One Operating System
Feb 10, 2006
Avoiding Java Exception Abuse
Dec 22, 2005
Java Perspective: Advanced Core Data
Dec 16, 2005
Java Perspective: Cocoa-Java Bridge
Dec 1, 2005
Java Perspective: Core Data and the Mac OS X Developer
Nov 23, 2005
Java Perspective: Key-Value Protocols, Behind the Magic of Mac OS X Development
Nov 11, 2005
Java Perspective: Cocoa Subclasses and Delegates
Nov 4, 2005
The Java Perspective: Cocoa's Interface Builder
Oct 21, 2005
Maven: Handling Multiprojects
Sep 23, 2005
Java, Maven, and Your Web Development Project
Aug 26, 2005
Ant User, Let Me Introduce Maven
Jul 29, 2005
Using JAAS Authentication with JBoss
Jun 17, 2005
Building a Custom JBoss Login Module
Jun 10, 2005
Testing Java Enterprise Applications with Cactus
May 20, 2005
Ant: Building a Better Build File
May 6, 2005
XDoclet: Entity Bean Relationships
Apr 8, 2005
The Java Developers' Introduction to XDoclet
Jan 7, 2005
An Introduction to Cactus
Dec 30, 2004
Using Multiple Databases with JBoss
Nov 24, 2004
JBoss Application Configuration Breakdown
Nov 5, 2004
Sortable Swing JTable
Oct 1, 2004
Building a Professional Swing JTable
Sep 10, 2004
Creating a Custom Java Swing Tablemodel
Aug 27, 2004
Adding Workflow Control to Your Java Applications
Mar 26, 2004
The Mac Developer Network
By on November 21, 2007 No Comments
Steve Scott of Late Night Cocoa has started a new podcast targeted specifically at Macintosh Developers.  One of the first episodes on this podcast is a roundtable of developers with several very well known developers starting with Daniel Jalkut, Uli Kusterer and Fraser Spiers along with myself.
Once more unto the [Beta Cycle], dear friends, once more
By on November 16, 2007 No Comments
Today marks the first semi-public release of iWeb Buddy, a new application that I have been developing for the past few months.  Unlike seSales or applications developed as a consultant, this development cycle has been a blur of activity.
Apple -- You Did Me A Disservice
By on October 26, 20073 Comments
Today is the day that most Mac users have been looking forward to for over a year.  Ever since Steve Jobs got up on stage last August and showed us Leopard for the first time.  Ever since that first peak almost all Mac users and I dare say ALL Mac developers have been looking forward to this release.  Unfortunately, today is also a day that Mac developers are going to be scrambling like rats on a burning ship.  This is because Apple decide to thumb their nose at us.
Further development of Bullfrog 2 for OS X
By on October 11, 2007 No Comments
Things are starting to speed up on the development of Bullfrog 2 previously mentioned here.  In our continuing disclosure about the development process, Jon Trainer has written a very informative article on how the actors are configured.
Starting over is hard to do
By on September 18, 2007 No Comments
I mentioned before that I am working with Jon Trainer of Outer Level to write Bullfrog 2 for OS X.  This development effort will be targeted at the as of yet unreleased OS X 10.5 Leopard.  We also promised to blog about the development as it moved forward.  Unfortunately not everything has been roses.
Sams Teach Yourself Mac OS X Tiger All in One

Like this article? We recommend
Sams Teach Yourself Mac OS X Tiger All in One

In this new article from a Java perspective, Marcus Zarra discusses Core Data, which is a new framework that was introduced by Apple with the release of OS X 10.4 Tiger. Core Data replaces the need for building data objects and helps manage storing data in a flat file, XML file or SQLite database. Marcus walks you through some of the basic usages of Core Data and tells you why it is so useful.

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:

  1. Open up the inspector while the Person NSArrayController is selected.
  2. Go to the Attributes tab.
  3. 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:

  1. Open up the ManagedObjectContext section.
  2. Change the Bind To to the Application Delegate.
  3. 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:

  1. 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.
  2. Open the bindings tab for the inspector of this column.
  3. Open the value section.
  4. Change Bind To to the Person NSArrayController.
  5. Ensure that the Controller Key is set to arrangedObjects.
  6. 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:

  1. Select the NSTextField that will be associated with the firstName attribute.
  2. Go to the bind tab in the Inspector window.
  3. Open the value section.
  4. Change Bind To to the Person NSArrayController.
  5. Ensure that the Controller Key is set to selection.
  6. 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.

  • Share ThisShare This
  • Your Account

Discussions

Make a New Comment

You must log in in order to post a comment.

Related Resources

Jennifer  BortelWin FREE iPhone Developer Books and Videos- Introducing @InformIT Giveaways
By Jennifer Bortel on February 5, 2010 No Comments

Apples’s recent iPad announcement made our hearts flutter so we couldn’t resist making an announcement of our own!

Today marks the first ever @InformIT Giveaway!

We’ll regularly post a video like this one profiling spectacular prizes we’re giving away—from books and videos to T-shirts and other exciting stuff. Check out the video below to see the giveaways for today, and then scroll down for more prize details and instructions on how to win them!

So Far So Good
By John Traenkenschuh on February 2, 2010 No Comments

So far, Win 7 is making a thoroughbred of what has been a plough mule laptop

Dustin Sullivan"Every OSX developer should have this book on their desk."
By Dustin Sullivan on February 1, 2010 No Comments

That was the sentence Mike Riley ended his recent Dr Dobb's CodeTalk review of Cocoa Programming Developer's Handbook with.

See All Related Blogs

Informit Network