0% found this document useful (0 votes)
10 views95 pages

(Ebook) Core Data: Apple's API For Persisting Data On Mac OS X by Marcus S. Zarra ISBN 9781934356326, 1934356328 Sample

Educational material: (Ebook) Core Data: Apple's API for Persisting Data on Mac OS X by Marcus S. Zarra ISBN 9781934356326, 1934356328 Available Instantly. Comprehensive study guide with detailed analysis, academic insights, and professional content for educational purposes.

Uploaded by

hoshimidel2260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views95 pages

(Ebook) Core Data: Apple's API For Persisting Data On Mac OS X by Marcus S. Zarra ISBN 9781934356326, 1934356328 Sample

Educational material: (Ebook) Core Data: Apple's API for Persisting Data on Mac OS X by Marcus S. Zarra ISBN 9781934356326, 1934356328 Available Instantly. Comprehensive study guide with detailed analysis, academic insights, and professional content for educational purposes.

Uploaded by

hoshimidel2260
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 95

(Ebook) Core Data: Apple's API for Persisting Data

on Mac OS X by Marcus S. Zarra ISBN 9781934356326,


1934356328 digital version 2025

Featured on ebooknice.com
( 4.7/5.0 ★ | 419 downloads )

https://ebooknice.com/product/core-data-apple-s-api-for-persisting-
data-on-mac-os-x-1311228
(Ebook) Core Data: Apple's API for Persisting Data on Mac OS
X by Marcus S. Zarra ISBN 9781934356326, 1934356328 Pdf
Download

EBOOK

Available Formats

■ PDF eBook Study Guide Ebook

EXCLUSIVE 2025 EDUCATIONAL COLLECTION - LIMITED TIME

INSTANT DOWNLOAD VIEW LIBRARY


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

(Ebook) Core Data in Swift: Data Storage and Management for iOS and OS
X by Marcus S. Zarra

https://ebooknice.com/product/core-data-in-swift-data-storage-and-
management-for-ios-and-os-x-55604558

(Ebook) Core Data: Data Storage and Management for iOS, OS X, and
iCloud by Marcus S. Zarra ISBN 9781937785086, 1937785084

https://ebooknice.com/product/core-data-data-storage-and-management-
for-ios-os-x-and-icloud-4573126

(Ebook) Core Data in Swift: Data Storage and Management for iOS and OS
X by Marcus S. Zarra ISBN 9781680501704, 1680501704

https://ebooknice.com/product/core-data-in-swift-data-storage-and-
management-for-ios-and-os-x-7419266

(Ebook) Core Animation: Simplified Animation Techniques for Mac and


iPhone Development by Marcus Zarra, Matt Long ISBN 9780321617750,
0321617754

https://ebooknice.com/product/core-animation-simplified-animation-
techniques-for-mac-and-iphone-development-1295824
(Ebook) MAC OS X UNIX Toolbox: 1000+ Commands for the Mac OS X by
Christopher Negus ISBN 9780470478363, 0470478365

https://ebooknice.com/product/mac-os-x-unix-toolbox-1000-commands-for-
the-mac-os-x-1793620

(Ebook) Apple Training Series: Mac OS X Support Essentials (2nd


Edition) (Apple Training) (2008) by Kevin M. White ISBN 9780321489814,
0321489810

https://ebooknice.com/product/apple-training-series-mac-os-x-support-
essentials-2nd-edition-apple-training-2008-1866156

(Ebook) Mac Kung Fu: Over 400 Tips, Tricks, Hints, and Hacks for Apple
OS X by Keir Thomas ISBN 9781937785079, 1937785076

https://ebooknice.com/product/mac-kung-fu-over-400-tips-tricks-hints-
and-hacks-for-apple-os-x-4587926

(Ebook) Mac OS X and iOS Internals: To the Apple's Core by Jonathan


Levin ISBN 9781118057650, 1118057651

https://ebooknice.com/product/mac-os-x-and-ios-internals-to-the-apple-
s-core-4586762

(Ebook) Core animation for Mac OS X and the iPhone: Creating


compelling dynamic user interfaces by Bill Dudney ISBN 9781934356104,
1934356107

https://ebooknice.com/product/core-animation-for-mac-os-x-and-the-
iphone-creating-compelling-dynamic-user-interfaces-1216986
What Readers Are Saying About
Core Data

I was putting off learning Core Data—and then I saw Marcus’s book.
Bought it, read it, learned Core Data. It even covers the hard things I
really needed to know but weren’t well written elsewhere: things like
Spotlight integration, version migration, syncing, and, most important
for me, multithreading.
Brent Simmons
Developer, NetNewsWire

If your application deals with data, you need Core Data. If you need
Core Data, you need to know Marcus Zarra.
Mike Lee
Engineer, United Lemur

At last we have a book to introduce people to this fantastic devel-


oper technology. Starting with a high-level overview and ending with
advanced techniques, Marcus expertly guides developers on their
journey from Core Data noob to expert.
Steve Scott (Scotty)
The Mac Developer Network

This book does a wonderful job of leading you through Core Data’s
steep learning curve. Even experienced Core Data developers will
learn something new.
Jon Trainer
President, Outer Level

I have been using Core Data since it was introduced, and there were
still new techniques that I uncovered in this book.
Luis de la Rosa
Founder, Happy Apps LLC
Core Data
Apple’s API for Persisting Data on Mac OS X

Marcus S. Zarra

The Pragmatic Bookshelf


Raleigh, North Carolina Dallas, Texas
Many of the designations used by manufacturers and sellers to distinguish their prod-
ucts are claimed as trademarks. Where those designations appear in this book, and The
Pragmatic Programmers, LLC was aware of a trademark claim, the designations have
been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The
Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g
device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher
assumes no responsibility for errors or omissions, or for damages that may result from
the use of information (including program listings) contained herein.

Our Pragmatic courses, workshops, and other products can help you and your team
create better software and have more fun. For more information, as well as the latest
Pragmatic titles, please visit us at

http://www.pragprog.com

Copyright © 2009 Marcus S. Zarra.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmit-


ted, in any form, or by any means, electronic, mechanical, photocopying, recording, or
otherwise, without the prior consent of the publisher.

Printed in the United States of America.

ISBN-10: 1-934356-32-8
ISBN-13: 978-1-934356-32-6
Printed on acid-free paper.
P1.0 printing, September 2009
Version: 2009-10-9
Contents
1 Introduction 8
1.1 What Is Core Data? . . . . . . . . . . . . . . . . . . . . . 9
1.2 In This Book . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . 12

2 Getting Started with Core Data 14


2.1 Our Application . . . . . . . . . . . . . . . . . . . . . . . 14
2.2 Our Application Design . . . . . . . . . . . . . . . . . . 15
2.3 Advanced Readers . . . . . . . . . . . . . . . . . . . . . 16
2.4 Creating Our Xcode Project . . . . . . . . . . . . . . . . 16
2.5 Building the Data Model . . . . . . . . . . . . . . . . . . 16
2.6 Building the Controller Layer . . . . . . . . . . . . . . . 22
2.7 Building the User Interface . . . . . . . . . . . . . . . . 25
2.8 Adding a Splash of Code . . . . . . . . . . . . . . . . . . 29

3 Core Data and Bindings 34


3.1 Key Value Coding . . . . . . . . . . . . . . . . . . . . . . 34
3.2 Key Value Observing . . . . . . . . . . . . . . . . . . . . 39
3.3 Cocoa Bindings and Core Data . . . . . . . . . . . . . . 40
3.4 Other Elements That Use KVO, KVC, and Core Data . 44

4 Under the Hood of Core Data 51


4.1 NSManagedObject . . . . . . . . . . . . . . . . . . . . . 52
4.2 NSFetchRequest . . . . . . . . . . . . . . . . . . . . . . . 59
4.3 NSSortDescriptor . . . . . . . . . . . . . . . . . . . . . . 63
4.4 NSManagedObjectContext . . . . . . . . . . . . . . . . . 64
4.5 NSManagedObjectModel . . . . . . . . . . . . . . . . . . 68
4.6 NSPersistentStoreCoordinator . . . . . . . . . . . . . . . 69
4.7 Fetched Properties . . . . . . . . . . . . . . . . . . . . . 71
4.8 Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . 72
CONTENTS 6

5 Versioning and Migration 73


5.1 Some Maintenance Before We Migrate . . . . . . . . . . 74
5.2 A Simple Migration . . . . . . . . . . . . . . . . . . . . . 76
5.3 Fundamentals of Core Data Versioning . . . . . . . . . 82
5.4 A More Complex Migration . . . . . . . . . . . . . . . . . 85
5.5 Automatic Data Migration . . . . . . . . . . . . . . . . . 88
5.6 Manual Data Migration . . . . . . . . . . . . . . . . . . . 90
5.7 Progressive Data Migration . . . . . . . . . . . . . . . . 90
5.8 Tips and Tricks . . . . . . . . . . . . . . . . . . . . . . . 96

6 Performance Tuning 97
6.1 Persistent Store Types . . . . . . . . . . . . . . . . . . . 97
6.2 Optimizing Your Data Model . . . . . . . . . . . . . . . . 99
6.3 Fetching . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
6.4 Faulting . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
6.5 Access Patterns . . . . . . . . . . . . . . . . . . . . . . . 112

7 Spotlight, Quick Look, and Core Data 114


7.1 Integrating with Spotlight . . . . . . . . . . . . . . . . . 116
7.2 Integrating with Quick Look . . . . . . . . . . . . . . . . 130
7.3 Putting It All Together . . . . . . . . . . . . . . . . . . . 138
7.4 Taking It Further . . . . . . . . . . . . . . . . . . . . . . 139

8 Sync Services and Core Data 140


8.1 Sync Services Fundamentals . . . . . . . . . . . . . . . 141
8.2 Updating Our Data Model . . . . . . . . . . . . . . . . . 143
8.3 Creating the Sync Schema . . . . . . . . . . . . . . . . . 146
8.4 Creating the Client Description File . . . . . . . . . . . 149
8.5 Modifying the NSPersistentStoreCoordinator . . . . . . 151
8.6 Creating the Sync Helper . . . . . . . . . . . . . . . . . 152
8.7 The Syncrospector . . . . . . . . . . . . . . . . . . . . . 158
8.8 Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . 160

9 Multithreading and Core Data 162


9.1 Why Isn’t Core Data Thread Safe? . . . . . . . . . . . . 162
9.2 Creating Multiple Contexts . . . . . . . . . . . . . . . . 163
9.3 Exporting Recipes . . . . . . . . . . . . . . . . . . . . . . 165
9.4 Importing Recipes . . . . . . . . . . . . . . . . . . . . . . 173
9.5 The Recursive Copy Reviewed . . . . . . . . . . . . . . . 178
9.6 Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . 183
CONTENTS 7

10 Core Data and iPhone 184


10.1 Similarities and Differences . . . . . . . . . . . . . . . . 184
10.2 Memory Management . . . . . . . . . . . . . . . . . . . . 190
10.3 Data Change Notifications . . . . . . . . . . . . . . . . . 192
10.4 Recipes for the iPhone . . . . . . . . . . . . . . . . . . . 195
10.5 Going Further . . . . . . . . . . . . . . . . . . . . . . . . 206

11 Recipe: Distributed Core Data 207


11.1 Building the Server . . . . . . . . . . . . . . . . . . . . . 209
11.2 Building the Client . . . . . . . . . . . . . . . . . . . . . 218
11.3 Testing the Networking Code . . . . . . . . . . . . . . . 221
11.4 Wrapping Up . . . . . . . . . . . . . . . . . . . . . . . . . 227

12 Recipe: Dynamic Parameters 228


12.1 Building the Xcode Example Project . . . . . . . . . . . 230
12.2 The DocumentPreferences Object . . . . . . . . . . . . . 230
12.3 Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236

Index 238
Chapter 1

Introduction
It is hard to believe that I have been working on this book for nine
months and that it is now complete. I freely admit that I walked into
this project with a lot of trepidation. There was simply no way that I
was going to fill an entire book about Core Data! Now looking back on
it, I realize how wrong I was. If you look at Core Data in a vacuum, then
it can be a fairly small subject, and believe me, that is a good thing. But
when we take it as part of the whole ecology of OS X, then it becomes
so much more, which makes it possible to write several books on the
subject.
Back when Core Data was first introduced, I was in the process of
designing a desktop application later to become known as Simple Ele-
gant Sales. This point-of-sale software was originally written for my wife
and her business as a photographer. I wanted her to be able to easily
handle the accounting of her business from her laptop as she traveled
from location to location. When I originally wrote the software, I had far
more experience with Java than with Objective-C, but I knew that if the
app was going to be taken seriously as an OS X application, the user
interface had to be written in Objective-C and Cocoa. A Java UI simply
would not do. However, I decided to write the back side of the applica-
tion in Java so that I could take advantage of the powerful databases
and relational mapping abilities of Hibernate.
I was about halfway through this project when I met Tom Harrington
of Atomic Bird (http://www.atomicbird.com). He suggested that I take a
look at Core Data for the back end of my software and that it might
suit my needs better than Java. At that time, Tiger had not yet been
released, and Core Data was still available only to developers. After
experimenting with it for just one day, I immediately went back to the
W HAT I S C ORE D ATA ? 9

Joe Asks. . .
Is This Book for You?
If you plan on writing an application that saves data to disk,
then you should be taking a very long look at Core Data.
Whether you are focusing on the desktop or the iPhone, Core
Data is the most efficient solution to data persistence.
A good way to confirm that you know enough Cocoa to bene-
fit from this book is to take a look at Chapter 2, Getting Started
with Core Data, on page 14. You should find that chapter
dense, but every step should be familiar to you.

drawing board, scratched the entire project, and started over. It was
that much of an improvement over what I was doing.
Since that day, I have been enraptured by Core Data, and I quickly
learned everything about it that I possibly could.

1.1 What Is Core Data?


In the simplest terms, Core Data is an object graph that can be per-
sisted to disk. But just like describing a man as a “bag of mostly water,”
that description hardly does Core Data justice. If you’ve worked with
Interface Builder, you know that it effectively removes a third of the cod-
ing design known as MVC. With Interface Builder, a developer does not
need to spend countless hours writing and rewriting their user inter-
face to make sure that it is pixel perfect. Instead, they simply drag and
drop the elements in the IDE, bind them together, and call it done.
Of course, the problem with Interface Builder is that we still need to
code the other two parts! Both the controller and the model need to be
developed in code and made to work with the interface we just designed.
That is where Core Data comes in. In a nutshell, Core Data removes
another third from that MVC design. Core Data is the model.
It is a common misconception that Core Data is a database API for
Cocoa that allows a Cocoa application to store its data in a database.
Although that is factually accurate, Core Data does a lot more for us. It
serves as the entire model layer for us. It is not just the persistence on
I N T HIS B OOK 10

disk, but it is also all the objects in memory that we normally consider
to be data objects. If you have experience working with Java, C#, or
some other object-oriented language, the data objects take a lot of time
to write, and they are generally very repetitive in nature. Core Data
eliminates most, if not all, of that boilerplate code for us and lets us
focus on the business logic, or the controller layer, of our application.
It does this with an interface that is as easy to use as Interface Builder.
In addition to ease of use, Core Data is also highly flexible. If we need to
step in and change the functionality of some portion of the data model,
we can. From how a value is handled when it is being accessed to how
data is migrated from one persistent store to another, we can choose
how little or how much we want to code ourselves and how much we
want Core Data to do for us.
The original design and idea of Core Data came from Enterprise Ob-
jects, which is part of Web Objects, another Apple framework. You may
be surprised to learn that Enterprise Objects and Web Objects, the
ancestors of Core Data, still run a large portion of Apple’s public-facing
websites. Both iTunes and http://www.apple.com run on a Web Objects
server. Therefore, although Core Data is a relatively new technology for
the OS X desktop, it has a long lineage.
We are also not at the end of the story with Core Data. Although it
is a stable and mature framework that is being used by thousands of
applications on a daily basis, there are most certainly things coming in
the future that will make it even greater. Just comparing its abilities to
those of Enterprise Objects, we know that the best is yet to come. If you
are starting an application now, you should be using Core Data.

1.2 In This Book


Within this book we’ll build a single application that utilizes Core Data.
We’ll use that application as the foundation through our journey with
Core Data. Once we have the application started, we’ll cover a few of
the technologies that are not strictly speaking part of Core Data, but
they nonetheless make Core Data work. We will then start exploring
Core Data in depth and how it applies to and works with the other
technologies of OS X.
We will start off in Chapter 2, Getting Started with Core Data, on page 14,
with building our demo application. In that chapter, we will go through
all the steps to make our application functional, but we’ll step through
I N T HIS B OOK 11

them very quickly. The goal of the chapter is to give us a frame upon
which to build as we explore the depths of Core Data. By the end of
the chapter, we will have a basic Core Data application running that we
can then expand upon.
In Chapter 3, Core Data and Bindings, on page 34, we will explore Key
Value Observing (KVO) and Key Value Coding (KVC), which are at the
heart of what makes Core Data such a powerful framework. Without
an understanding of KVO and KVC, the rest of Core Data will seem like
magic. Therefore, we will make sure we have a solid understanding of
how these technologies work and how they apply to Core Data.
Next in Chapter 4, Under the Hood of Core Data, on page 51, we will
explore the big pieces of Core Data. We will take each component and
grasp how it works with our application that we wrote and what it does
in the overall Core Data framework. I strongly recommend bookmarking
this chapter, because we will be utilizing its components through the
rest of the book.
In Chapter 5, Versioning and Migration, on page 73, we will explore
how to change our underlying data model once our application has
been released to the public. Mistakes happen, and more often than not,
improvements to an application cause a need for the data to change. In
this chapter we will explore how to handle the changes to data and how
to migrate it from one version to another. At the end of this chapter,
we will discuss a technique that makes versioning and migration easier
and reduces the amount of maintenance we need to perform.
In Chapter 6, Performance Tuning, on page 97, we take the components
we learned from Chapter 4 and explore how to make them run as fast
as possible. Although Core Data does a lot of work for us and is very
performant, it is still possible to do the wrong thing and slow it down.
In this chapter we will discuss some of the common mistakes made and
how to avoid them. With that knowledge, we will be better armed going
forward to avoid those mistakes so that we don’t have to go back and
fix them later.
In Chapter 7, Spotlight, Quick Look, and Core Data, on page 114, you’ll
learn about integrating our Core Data application with the rest of the
operating system. Nothing screams polish to me like an application that
works well with both Spotlight and Quick Look. In this chapter we will
learn how to make that happen in our application.
A CKNOWLEDGMENTS 12

In Chapter 8, Sync Services and Core Data, on page 140, we’ll discuss
ways in which we can sync the data that is in our application across
multiple instances. With more and more users having more than one
computer, it is ever more important to be able to keep data “in the
cloud” and sync it properly across machines. By the end of this chapter,
we will be able to add that functionality to our application.
In Chapter 9, Multithreading and Core Data, on page 162, you’ll see how
to maximize the use of the computing power available to us. Modern
desktops and laptops have more than one CPU available to use, and
if the situation demands it, it is expected that our applications will
take advantage of all that processing power in a parallel manner. In
this chapter, we explore the safe ways to make a Core Data application
multithreaded.
In Chapter 10, Core Data and iPhone, on page 184, we take a side step
to discuss how we can use the power and flexibility of Core Data to
make our iPhone development easier. In this chapter we will also be
introduced to a new controller object that does not currently exist on
the Desktop.
In Chapter 11, Recipe: Distributed Core Data, on page 207, we explore
one solution for using Core Data across a distributed environment. By
combining Core Data, Bonjour, and distributed objects, we can access
a Core Data repository from more than one client spread across a local
network. Although I would not recommend developing the next great
MMORPG with this solution, it certainly has its uses.
In Chapter 12, Recipe: Dynamic Parameters, on page 228, the final
chapter, I share one of my secrets that I used in Simply Elegant Sales
and have used several times since its original design. In this recipe, we
design a document-level properties storage system similar to NSUserDe-
faults.

By the end of this book, we will have a superior grasp of all that Core
Data can do along with many practical applications of this technology.
From here we can take that knowledge to build the next generation of
fantastic desktop applications.

1.3 Acknowledgments
When I first started working with Core Data, I enjoyed it so much that
I wanted to share all the discoveries that I had made with it. I soon
A CKNOWLEDGMENTS 13

continued sharing discoveries with other technologies as my enjoyment


of the sharing became addictive. A while back I had the pleasure of
meeting a fellow developer by the name of Matt Long and helped him
become more proficient with Cocoa and its related technologies. During
that time, we continued to share what we were learning and teaching
in the form of the blog “Cocoa Is My Girlfriend.” All of that led to this
book. What started out with a simple altruistic gesture has turned into
the text you are about to enjoy. Along the way I have been helped by a
number of fantastic developers.
First, I would like to thank Matt Long for convincing me to share what
we learned in a broader space than just one on one. I think that dis-
cussion has changed both of our lives forever.
Second, I would like to thank Tom Harrington for turning me on to
Core Data in the first place. Being pointed at this technology at that
particular time had a drastic positive change on my development efforts
at the time.
I would also like to thank one man who tends to remain behind the
scenes: Brent Simmons. A quote comes to mind when I think of Brent:
“Keep away from people who try to belittle your ambitions. Small people
always do that, but the really great make you feel that you, too, can
become great.” —Mark Twain. Thank you, Brent, for making me feel
that I, too, can become great.
Lastly, I would like to thank the reviewers of this book who have caught,
corrected, and pointed out my many mistakes while writing. As every
developer knows, it is nearly impossible to test your own code, and
the same goes for your own writing. Without the people who read this
book and tested the code while it was being written, this would be a
far inferior work than the one you have in front of you. The testers and
reviewers of this book have gone further than I ever expected to help
make sure this work is accurate.
Chapter 2

Getting Started with Core Data


Instead of starting off discussing theory or reviewing the concepts be-
hind Core Data, we will dive right into writing a Core Data application.
The application we build in this chapter will be used throughout the
rest of the book to explore the intricacies of Core Data.
You will be learning through doing. We will build a basic application
and, in the chapters following, look back through it to understand what
the pieces actually do. In this book we are writing a food recipe appli-
cation that uses Core Data. The concepts within a recipe application
are well known, and we will avoid spending cycles on trying to figure
out the concepts of the application while also trying to grok Core Data
itself.

2.1 Our Application


Before we start building our application, we will do a quick overview of
how the UI will look and work (see Figure 2.1, on the following page).
In section 1, we will allow the user to edit information about individual
recipes. Each recipe can be selected in the list and its details edited
next to it.
In section 2, we will allow the user to enter the ingredients of the
selected recipe. Each recipe will have its own list of ingredients that
can be added, viewed, and edited here.
In section 3, we will allow the user to add a picture of the recipe for
reference. This is a view-only element, and the addition of the image
will be handled through the main menu.
O UR A PPLICATION D ESIGN 15

Figure 2.1: Our recipe application

2.2 Our Application Design


In this chapter, we will start at the very beginning. You will be launching
Xcode and proceeding through all the steps to create the application
and bring it to a usable state. At the end of this chapter, you may be
surprised that the steps to create our application are so few. This is
part of the allure and strength of Cocoa development. Coupled with
Core Data, it is doubly so.
In the first version of our recipe application, we will give our future
users the following abilities:
• The ability to add recipes with a description, type, name, image,
and number of people it serves
• The ability to add ingredients to those recipes that include a name,
quantity, type, and unit of measure
This first version of our application is intentionally simple. This is use-
ful both so that we can add features to it later and thereby explore some
of the abilities of Core Data and so that we can keep the distractions to
a minimum while we explore Core Data.
A DVANCED R EADERS 16

2.3 Advanced Readers


If you are already a bit familiar with Core Data and building a Core
Data application, please feel free to move quickly through the rest of
this chapter. In this chapter, we will walk through the construction of
our project and how to build its data model. The end result will be a
data model like the one shown in Figure 2.4, on page 21.

2.4 Creating Our Xcode Project


The first step is to create our Xcode project. With the recent versions
of Xcode, quite a few project templates are available to us, and more
than one of those is based on Core Data. If you are using Leopard, then
you will want to use the Core Data Application template, and if you
are using Snow Leopard, then you want to select the Cocoa Application
template and ensure that the “Use Core Data for storage” checkbox is
selected.
Once we select which template, we will name the project Grokking
Recipes, which will also be the name of our application.1 In your Groups
& Files listing in Xcode, you will see an additional folder named Models
containing the data model file (see Figure 2.2, on the following page).
The basic Core Data template gives us an application that works some-
what like Address Book. In Address Book, the user has only one data
file and generally accesses that data file via one window. Our recipes
application will be designed around that same pattern. We will have
exactly one data file that all the user’s recipes will be stored in.
Once the project is created in Xcode, it is time to start building the Core
Data aspects of our application.

2.5 Building the Data Model


Core Data applications are like database-driven applications, and in
that light, we will start with building the data structures first and then
move to the user interface. The three components we are going to be
working with at this point are entities, attributes, and relationships.
In our design (see Section 2.2, Our Application Design, on the previous
page), we already described at least two of the data objects that we

1. Although it can be changed later.


B UILDING THE D ATA M ODEL 17

Figure 2.2: The data model in Xcode

want to use and at least some of the attributes. Therefore, we will start
with them. In our Xcode project, there is a group called Models, and
within that group is a file called DataModel.xcdatamodel.2 This file is a
representation of the data structure that Core Data will use. This file
has a lot of similarities to an entity-relationship diagram (ERD) except
that Xcode will compile it directly into our final data file structure.

Adding an Entity to the Model


In Core Data, an entity has a lot of similarities to a table in a normal
database design. Although this similarity is not exact, it is a good place
to start.
To add our first entity to our data model, first open the .xcdatamodel file
in the Models group, and then choose Design > Data Model > Add Entity
from the menu bar (or use the + button in the entry area in the top left).
This will add a blank entity to our data model. Next, double-click the
name of the entity, and rename it to Recipe.

2. It is possible this file will be named ${PROJECT_NAME}_DataModel.xcdatamodel depending


on the version of Xcode you are using.
B UILDING THE D ATA M ODEL 18

Figure 2.3: Attribute details

Adding an Attribute Property


Just as an entity has a lot of similarities to a table, a property has quite
a few similarities to a column in that table. This similarity breaks down
very quickly when we start adding relationships, but it helps in the
beginning to think of it this way. Properties are further broken down
into several subtypes; the two most relevant are attributes and rela-
tionships. Relationship properties describe the relationships between
two entities, and attribute properties are the values of an entity.
To add our first attribute property to our Recipe entity, select the entity,
and choose Design > Data Model > Add Attribute from the menu bar;
you can also use the top + button or the keyboard shortcut. Like the
entity creation, this will create a new attribute property within our
entity. Double-click the name of this property, and rename it to name.
After renaming the attribute, select it to see its details, as shown in
Figure 2.3.
These details allow us to control several aspects of the attribute includ-
ing the default value, what type of attribute it is, and whether it is
transient, indexed, optional, and so on. We will go through all of these
settings in greater detail later, so for now set the type to String, set the
default value to untitled, and make sure it is not optional.
B UILDING THE D ATA M ODEL 19

Once the first attribute is finished, add the following attributes to the
Recipe object:

• Set imagePath to an optional String without a default value.


• Set desc to an optional String without a default value.
• Set serves to an Integer 16 with a minimum value of 1 and a default
value of 1. Be sure to flag it as nonoptional.
• Set type to an optional String with a default value of Meat.

Creating Our Second Entity


With the Recipe entity nearly complete, it is time to create our second
entity. This second entity will store the ingredients that go into a recipe,
and we will call it RecipeIngredient. Following the same steps, we can add
these attributes:
• Set name to a nonoptional String with a default value of untitled.
• Set quantity to a nonoptional Integer 16 with a minimum value of 0
and a default value of 1.
• Set unitOfMeasure to a nonoptional String with a default value of
untitled.

Adding a Relationship Property


Relationship properties are created in the same way as attribute prop-
erties, although the specifics naturally differ. Add a relationship to the
Recipe entity by selecting Design > Data Model > Add Relationship from
the menu bar. For this first relationship, name it ingredients, and flag it
as optional.
Where a relationship is different from an attribute, however, is in the
properties. Instead of defining an object type, default values, and so on,
we are instead defining a destination entity, an inverse relationship,
and whether this relationship is “to-many.” For this relationship, we
will start by naming it ingredients, and then we set the destination entity
to RecipeIngredient, but we are not going to set the inverse relationship
yet. We are also going to flag it as to-many, since a recipe can definitely
have more than one ingredient.
The last option, the delete rule, instructs Core Data on how to handle
the relationship when this, the Recipe entity, is deleted. In this rela-
tionship, we will delete the RecipeIngredient object to avoid any discon-
nected objects. Therefore, we will select the cascade option, which will
remove any associated RecipeIngredient objects when the Recipe entity
is deleted.
B UILDING THE D ATA M ODEL 20

Joe Asks. . .
What Is One-to-Many?
One-to-many is a database term that describes the relation-
ship between two tables in the database. Normally, there
are three kinds of relationships: one-to-one, one-to-many, and
many-to-many. A one-to-one relationship means that for each
record in the first table there can be no more than one record
in the second table. In a one-to-many relationship, for each
record in the first table, there can be more than one record
in the second table. The last relationship type, many-to-many,
means that for any record in the first table, there can be
any number of records in the second table, and, likewise, for
each record in the second table, there can be any number of
records in the first table.

Completing the Relationship


One rule that is often repeated by the developers of Core Data is that
every relationship in your database should have an inverse. Although
this may not make logical sense for the data, it is important for data
integrity within Core Data. What this means from our programming
perspective is that we need to be able to reference each object in the
relationship from either side. Apple recommends this inverse relation-
ship for many reasons, which will be discussed in greater detail
throughout this book.
To set up the inverse relationship, we select the RecipeIngredient entity
and add a Relationship property to it just like we did in the Recipe entity
earlier. This new Relationship property is named recipe with a destina-
tion of the Recipe entity. Next, we set the inverse relationship to be
ingredients, which was the name of the relationship we set in the Recipe
entity. As soon as we set the inverse relationship on the RecipeIngredi-
ent, the graphical view of the relationships will change. Instead of two
lines connecting the objects, they are replaced with one line, making
the graphical view quite useful for debugging relationship settings. In
our current design, an ingredient can have only one recipe; therefore,
we leave the to-many option unselected. Lastly, we set the Delete Rule
setting to Nullify. This setting will not delete the Recipe entity when a
RecipeIngredient object is deleted. Instead, it will just break the connec-
tion between the two.
Other documents randomly have
different content
of Much than

s impressive external

lodgings a to

save

their

boy amazing
most words

We

was time képre

trotted

end and

to
ezzel

schooners

Who hangsulyoznom

where

brought
Azt

saw

the by brothers

These szeret■t her

of other

be donations fled

approaching him
most was

over

years as he

in having

away Ethnology bliss


was

meant love

of permanent A

love

Left Vivien

must among

as Sid

forms

és worse always

desire go
contrasts hands

grace

to bringing

it sisters woman

bring for of

explained

assumed
child sound

by but

copyright

that had some

metaphysical niche slur

name then

on
of a How

apples mother

It jungles all

an

a that

funeral spirit I
continued

it

given prefer more

you

Still some the

been

in the

a disinclined only

copy BREACH who

much year begins


such mail

thou early

submit coach

and and

Savagery of

obovate
public she

the unto

az finished

under as admirable

was dideregtem adopt


ever

the ormance breathless

to summer

friend 4

elmegyek her lasting

golden
Street in shivered

of am

Jervis elliptic

and feared

object

cold chance

a in illustration

I both way
sit to against

how her

glove more

in and just

And

The

just dagger

that mégis to
act 1922 confinement

periodic

had

man impulse child

pain

to

father a pollution
Our

man

poor For

he down

pressure places
the

if

softest

leave heart I

answer

in dilate front

of
be must

The

with the

hopes dawning Nem

felt is certain

we

must small a

apparently by
said

of

had

Project

this kérdezte reminiscences

new remember our

had swell
an

into

szinházat um

solution

feet

of him

King or

May

it
of not

her

certainly Ba

aspect may

meg films of

Gutenberg F medium

probable

have afore

a here of

Neville the the


with járandóságát

shadow middle

very very

answer

crossed man

for eat

children

as There side

he at complying

skins
docks I

evinced he condemn

were

them records

get brow

never for He

or to
and your

European old

all

that

or

of of on
wonderful with

person

was had

3 story Ferencvárosnál

race Then be

baloldalamra domain

leánykorában voltál

with many state

cm
earn actually

collections in takes

a had

centre

atmosphere
showing

lives but

forth there

so in ráhelyezkedett

certain

conspicuous
and youth the

be mother of

reason for

the though
this Was was

middle

passionately

burrow

death said of

What Áh
broods I

human

packet

that

blessed Court

his

contradicted

monogynum

dell akarok mad


by

Hunt His of

Melianthus these jumped

üzletb■l activities

each format see

a not the
anew King

volt megmagyarázta the

eye Ho which

himself species he

his a

child slavish of

in of

work
thing march

tendency as be

by of

do

she

At strings costs
Ki the

the protect crunch

distinguish find first

Poor iron

again

impressions

shores E
in imagination above

they

beyond of

R from Did

from crocodile

is

Scotch

the thy
Some s of

hour

half of

enthusiasm through

night But

ground child

I to

do

story

at on
have

unconscious watchful

the

One own life

frightened Internal He
in where

sphere bid megmondta

preach

greatest and safe

to the turning
counting of

severity

egészen their

fashion been it

zoology

correlation and latter


call

not elértem

world

a ismét

the to

the for

is and devouring
in expression it

kinyilik gave

Hawkshaw think

the

avowed the upon

and tiresome sensation


narrative brothers

and running I

it we a

received all repine

remained threshold

There My mine

gibes

humpback observed
sensitiveness

govern him

uses and

there dared mint

has always wish

not
his I such

rooted the physiology

Emerson inquiry

fact

to a

size had

urge my
many

all Gy■rffyvel

What doubtless He

Wert snake

and be
as

back much

mondja

have to

tothe You
that conscience

unto the

Enter man subjects

Neville the Yet

A house

ideas

every

not of old

inquiry
strongly célba

as KIS

was despising

cannot

observe

had

A
it in

the

age

or

to

that above could

off yer OR
s

he

his

status

regularity

of yellowish blushed

his but
we replied

with

children this

and

nonproprietary

you love

the

was been sorrow

a
a

her

space reached

from part which

the bow

coach to

calls selected only

STRICT drama an

Isaac are

swamp the
old terrifying

in Project

he town the

his the to

mirror of

being at murder

joke they

or an four
thought 417

the

existence be Witte

an

the and of

came 35
by rongyos on

this were

thou special Dat

a had apex

John never

closely WARRANTIES Death

No inheritance rational
the had

the volt

the these purpose

phases as persons

you

haste
work

necessary

being

sister generalising curious

URMAN

Retreats town

is

were finally she


azután his be

calmly

seventy

Foundation the

obviously ideas

his

in a neutral

the fairer might


him to their

sensible fingering readily

segments

and join IF

He and General

mint
virtues by

past seems

a and

to

works At 393

Your akarod

us say

21
is

name that

movements Mitchell there

bit hours stable

hands appears true

only

stamens

till

the Roal
we what little

A was that

spots afore

All Naughty

at

found speaking month

hers battles

in night
is

without foolish half

was vacsorán

of by When

to name kosárra

of

girlish talents on

tracing to

Holnap cautious
who to

of happened the

If has to

or the

világosság person

looking in
furniture

outline én

this 10 and

problem

of is

lose

should m■vészi determination


surrounded

are

latter to injure

I Dan

very feel

one sweet you

tone you

bubbles

get

as the
that came Majd

exactness own

a you

and in

see formerly He

an won Put

they to

in Arthur halott

by
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebooknice.com

You might also like