Core Data for iOS Developing Data-Driven Applications for the iPad, iPhone, and iPod touch

by ;
  • ISBN13:


  • ISBN10:


  • Edition: 1st
  • Format: Paperback
  • Copyright: 2011-05-24
  • Publisher: Addison-Wesley Professional
  • Purchase Benefits
  • Free Shipping On Orders Over $35!
    Your order must be $35 or more to qualify for free economy shipping. Bulk sales, PO's, Marketplace items, eBooks and apparel do not qualify for this offer.
  • Get Rewarded for Ordering Your Textbooks! Enroll Now
List Price: $39.99 Save up to $1.60
  • eBook
    Add to Cart


Supplemental Materials

What is included with this book?

  • The eBook copy of this book is not guaranteed to include any supplemental materials. Typically, only the book itself is included. This is true even if the title states it includes any access cards, study guides, lab manuals, CDs, etc.


The first comprehensive, readable reference for programming data-driven apps with Core Data on the iPhone 3.0 SDK provides in-depth, hands-on tutorials that walk developers through the entire process of building three Core Data-based iPhone applications.

Author Biography

Tim Isted has been writing software for Macintosh computers since 1995. He also builds web applications using Rails, PHP, and .NET and has been known to develop for Windows machines too. Also a professional musician and singing teacher, he tries to divide his time fairly equally between conducting, accompanying, teaching, and writing software. Previous musings on Core Data for desktop development can be found on his blog at www.timisted.net, and he is also co-organizer of NSConference, a new Mac developer conference taking place in both Europe and the USA.


Tom Harrington switched from writing software for embedded systems and Linux to Mac OS X in 2002 when he started Atomic Bird, LLC. After six years of developing highly regarded Mac software he moved to iPhone in 2008. He develops iOS software on a contract basis for a variety of clients. Tom also organizes iOS developer events in Colorado. When not writing software he can often be found on his mountain bike. His website is www.atomicbird.com.


Table of Contents

Preface   ix

Part I  Introduction

1  An Overview of Core Data on iOS Devices    3

A Little History    3

The Birth of Core Data     4

Why Use Core Data on iOS?     4

Relationship Management     4

Managed Objects and Data Validation     5

Undo and State Management    5

Core Data iOS and Desktop Differences    5

The Fetched Results Controller     6

Core Data Case Studies     6

MoneyWell for iPhone     6

Calcuccino     7

Associated Press     8

2  A Core Data Primer    9

Persisting Objects to Disk     9

The Core Data Approach     10

Entities and Managed Objects    10

Relationships    11

Managed Object Contexts    12

Fetching Objects     14

Faulting and Uniquing     14

Persistent Stores and Persistent Store Coordinators     15

Examining the Xcode Core Data Templates     15

The Navigation-Based Project Template    16

The Data Modeler     16

Setting up the Core Data Stack     17

Running the Application    20

A Quick Look at the RootViewController Code     20

Summary     21

3  Modeling Your Data    23

Managed Objects and Entities     23

Dividing Your Data into Entities     24

Core Data in Model-Object Terms     24

Data Normalization     25

Storing Binary Data     27

Working with Xcode’s Data Modeler     28

Creating Entities     29

Creating Properties     31

Creating Relationships     35

Summary     37

Part II  Working with Core Data

4  Basic Storing and Fetching    41

Creating New Managed Objects     41

Saving the Context    42

Fetching Saved Managed Objects     44

Deleting Managed Objects    45

Working with Table Views     46

The Random Dates Application Project     47

The Random Dates Data Model     48

Basic RootViewController Behavior     49

Fetching the Random Date Objects     51

Displaying the RandomDate Objects     52

Deleting the RandomDate Objects    54

Custom Managed Object Sub-Classes    54

Creating and Setting a Custom Class for a Managed Object     56

Summary     60

5  Using NSFetchedResultsController    61

Introducing NSFetchedResultsController    62

Creating an NSFetchedResultsController     62

Supplying Information to Table Views    64

The Number of Sections and Rows     65

Returning the Cell for an Index Path     67

Returning Information about Sections     68

Handling Underlying Data Changes    69

Caching Information    72

Using an NSFetchedResultsController in the Random Dates Application    73

Subclassing NSFetchedResultsController     80

Summary     85

6  Working with Managed Objects    87

Basic Managed Object Subclass Files     87

Creating the Random People Project     88

Managed Object Class Interfaces    89

Managed Object Class Implementations     90

Configuring the Random People Application    93

Displaying the Information    95

Data Validation     99

Validating Individual Properties    99

Validation Based on Other Properties     101

Validation Prior to Deletion     104

Fixing the Random People Application     105

Working with Transient Attributes    105

Modifying the Data Model    106

Adding to the AWPerson Interface and Implementation    106

Adding a Getter Method for the Transient Property     108

Adding a Setter Method for the Transient Property     110

Using the UIColor Property    111

Working with Transformable Attributes    113

The Managed Object Lifecycle     114

Initializing Non-persistent Properties     114

Summary     116

7  Working with Predicates    117

Predicate Basics     117

Creating Predicates Using Format Strings    118

Predicate Variables     120

Predicate Comparison Operators    122

Key Paths    123

Comparing Strings     124

Compound Predicates    126

NSCompoundPredicate     128

Sets and Relationships     129

Examining SQL Queries    130

Adding a Search Display Controller     130

Setting a Fetch Predicate     132

Modifying the Search Predicate    136

Adding a Search Scope Bar Filter    139

Summary     141

8  Migration and Versioning    143

The Migration Problem    143

Changing the Data Model    144

Multiple Data Model Versions and Lightweight Migration    145

Creating Data Model Versions    145

Enabling Lightweight Migration     146

Renaming Entities and Attributes     148

Supplying Renaming Identifiers    150

Keeping Track of Multiple Versions     151

Mapping Models     152

Custom Entity Migration Policies     156

Summary     160

9 Working with Multiple View Controllers and Undo    163

Editing Managed Objects    163

Keeping Track of the Managed Object to Edit    164

Updating a Managed Object’s Properties     168

Validating Managed Objects     171

Working with Undo    174

Multiple Managed Object Contexts     175

Merging Changes from Other Managed Object Contexts    178

Changing Managed Object Values Whenever the Control Values

Change    179

Resetting a Managed Object Context     181

Using the Editor Controller to Add New Objects    182

Summary     183

Part III  Building a Simple Core Data Application

10  Sample Application: Note Collector    187

The Note Collector Application     187

Creating the Note Collector Project    188

The Application Data Model     188

Modeling an Abstract Entity     189

Modeling Sub-entities    189

Creating Managed Object Class Files     190

Configuring the RootViewController    192

Displaying the Contents of a Collection    195

Keeping Track of the Collection to be Displayed     196

Examining the Contents of a Raw Data File    200

Setting and Editing an Item Name    202

Creating the New View Controller    203

Displaying and Editing Notes    210

Supplying a Pre-Populated Data Store    217

Working with a Data Store in the Application Bundle    217

Summary     219

Part IV  Optimizing and Troubleshooting

11  Optimizing for iOS Performance and Memory Requirements    223

Performance, Optimization, and Speed     224

Data Store Types    224

Binary and Memory Data Stores     225

SQLite Data Store    225

Monitoring SQLite Stores     225

Optimizing Fetching    230

Setting Fetch Limits    230

Optimizing Predicates    231

Pre-Fetching Relationships     233

Pre-Fetching Any Object    234

Pre-Loading Property Values     235

NSFetchedResultsController and Sections     235

Managing Faulting    235

“Safe” Fault-Free Methods     236

Preventing Property Loading    237

Batch Faulting    237

Re-faulting Objects     237

Managing BLOBs    238

Putting BLOBs in the Entity That Uses Them     239

Putting BLOBs in a Separate Entity    240

Putting BLOBs in External Files     242

Monitoring Core Data with Instruments     245

When Not to Use Core Data    248

Other Memory Management Tips     248

Don’t Use an Undo Manager If You Don’t Need It     249

Resetting the Managed Object Context     249

Summary     249

12  Troubleshooting Core Data    251

Your First Core Data Error     251

The Missing Model     254

Classes Not Found?    255

Core Data Threading Issues     257

Basics of Core Data Multithreading     257

Coordinating Data Between Threads     258

When Threads Collide, or Handling Data Conflicts     260

Danger! Temporary ID!    264

Problems Using Managed Objects     265

Crashing When Setting Property Values    265

If Custom Accessor Methods Aren’t Called     266

Managed Object Invalidated     267

Faults That Can’t Be Fulfilled     268

Problems Fetching Objects     269

Trouble Sorting Data During Fetches     269

Fetch Results Not Showing Recent Changes    270

Summary     270

Index    271



Rewards Program

Write a Review