Today, virtually every non-trivial iPhone and iPad app must manage data-quickly, smoothly, reliably, and with minimal impact on the CPU to conserve battery life. Core Data, Apples ready-made data persistence layer, can help you achieve all these goals. In Core Data for iOS, two leading iOS developers teach you the entire Core Data framework from the ground up. Writing for intermediate-to-advanced iOS developers, Tim Isted and Tom Harrington thoroughly explain how Core Data is used on iOS devices, introduce each of its primary classes, and show how they interact to provide amazing functionality with minimal configuration. Youll learn how to store, fetch, and validate data; provide it efficiently to views; and much more. Isted and Harrington first give you a firm grounding in the technology, and then present real-world examples. They present multiple sample projects, as well as a start-to-finish, chapter-length case study.
Coverage includes * Understanding Core Datas features, classes, and interactions * Using Core Data in MVC-based iOS app development * Mapping relational data to object models, and building them with Xcode 4s Data Modeler * Working with managed objects and using UITableView to display them * Creating predicates to match numbers, data, and objects * Maintaining compatibility across versions of an apps data model * Tracking managed object contexts across view controllers * Using Core Datas automatic Undo functionality * Integrating abstract entities, entity inheritance, and multiple view controllers into a complete app * Optimizing for iOS devices tight memory limits * Diagnosing and fixing common Core Data problems Introducing Addison-Wesleys new Core Frameworks Series, written for experienced iOS developers by world-class Mac and iOS developers, these are the first comprehensive, code-rich reference guides to Apples Core Frameworks.
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 Xcodes 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 Objects 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 Dont Use an Undo Manager If You Dont 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 Arent Called 266 Managed Object Invalidated 267 Faults That Cant Be Fulfilled 268 Problems Fetching Objects 269 Trouble Sorting Data During Fetches 269 Fetch Results Not Showing Recent Changes 270 Summary 270 Index 271