Chapter 5. Putting Core Data to Work

In This Chapter

  • Using Core Data

  • Figuring out fetch requests

  • Adding, saving, and loading Hotel objects with Core Data

  • Supporting Undo

When you have the infrastructure in place, as spelled out in Chapter 4 of this minibook, you still need to do one more thing to get Core Data up and running: You need to actually create the Managed Object Context in your application.

After you do that, you need to send a message to the Managed Object Context to create and delete objects as well as support iPhone's Shake feature to do undos and redos. You'll also create fetch requests to retrieve data using the Managed Object Context.

I show you how to do all of that in this chapter. But before you start, I want to explain the last major concept you need to understand to use Core Data: the fetch request.

Understanding the Fetch Request

A fetch request is an object that specifies what data you want, for example, "all Hotels ordered by city," or "all Hotels in Needles, California."

A fetch request has three parts:

  1. The name of an entity. (You can only fetch one type of entity at a time.)

  2. A predicate object that specifies conditions that objects must match.

  3. An array of sort descriptor objects that specifies the order in which the objects should appear.

You send a fetch request to a Managed Object Context, which gets the objects that match your request (possibly none), adds them to the Managed Object Context, and returns them to the application. If a context already contains a managed ...

Get iPhone® Application Development All-In-One For Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.