Online Book Reader

Home Category

Learn Objective-C on the Mac - Mark Dalrymple [67]

By Root 964 0
Core Data. Core Data’s functionality is orthogonal to Cocoa Binding’s; whereas Cocoa Bindings lets you eliminate some boring controller code, Core Data takes care of a lot of the “plumbing” that you’d otherwise have to write for your model classes, giving you a storage back-end, built-in Undo/Redo support, and much more. Together, Cocoa Bindings and Core Data can get you building so much software, so effectively, you’ll be making other people’s heads spin!

Chapter 7

Core Data Basics

In earlier chapters, we’ve shown you various ways that Cocoa lets you display data in view objects, from manually getting and setting values based on the contents of model objects to having the data automatically synchronized between model and view objects using Cocoa Bindings, which eliminates the need for a lot of boring controller code. Now it’s time to learn about Core Data, a powerful framework that gives your model objects a complete set of built-in capabilities. We’ll start by telling you a bit about what Core Data is and how it fits in with the other parts of Cocoa. Then you’ll use Core Data to create a full-featured database application called MythBase, including a GUI that allows you create, search, edit, and delete entries, all without writing a single line of code (see Figure 7-1 for a shot of MythBase in action). Then we’ll explore some of the code resources that are created for you automatically when you create a Core Data project, and finally we’ll show you how to add functionality (“business logic”) to your model objects.

What You’ve Been Missing


All of our examples in previous chapters have used instances of NSMutableDictionary in the place of real model objects. What do we mean by “real model objects?” Well, besides just being able to hold onto pieces of data, which are accessible using field names or keys (something that NSMutableDictionary does well enough), real model objects should include some of the following features:■ Archiving. Model objects should have access to a built-in mechanism for being saved to disk, and later reloaded.

■ Business Logic. There should be a way to give a model object custom behavior that operates in response to input values.

■ Validation. Each model object should be able to automatically validate input values.

Figure 7-1. The MythBase application, in all its glory

In the past, Mac application developers following MVC principles would typically have to roll their own solutions for these common needs, but Core Data provides all of this and more. Besides the features listed previously, Core Data also provides additional key features:■ Undo/redo support. Core Data’s mechanisms for handling values are tied into the standard Undo facility in Mac OS X. Having this built in to your model classes saves you the additional effort of implementing this common functionality yourself.

■ Integration with Cocoa Bindings. Together with Cocoa Bindings, Core Data provides you with a mechanism for connecting views to models using generic controller objects, thereby eliminating a lot of boring glue code.

Taken together, all of these features provide the core of your application with some sturdy infrastructure. You can use Core Data to build GUI apps (with or without Cocoa Bindings), command-line tools, games, or any other sort of software system that can be specified using traditional object modeling techniques. In other words, almost any application at all.

Creating MythBase


Now, let’s get started creating MythBase, a GUI application that lets us maintain a database of mythological figures. We’ll use Core Data for the model layer, and Cocoa Bindings to handle most of the controller functionality. There will be some new concepts and terminology to tackle, and we’ll cover each piece as we get to it.

In the first iteration, we’ll define a model for our application using a special tool inside Xcode, and create a simple GUI using an assistant in Xcode. In the second iteration, we’ll refine the GUI to improve the user experience a bit. Then, after explaining some other

Return Main Page Previous Page Next Page

®Online Book Reader