Pro Core Data for iOS.pdf
iOS CoreData 一本好书,smartrookie ■ Chapter 1: Getting Started .................................................................................................................... 1 ■ Chapter 2: Understanding Core Data .................................................................................................. 27 ■ Chapter 3: Storing Data: SQLite and Other Options ............................................................................ 57 ■ Chapter 4: Creating a Data Model................................................. .................................................... 107 ■ Chapter 5: Working with Data Objects ............................................................................................. 129 ■ Chapter 6: Refining Result Sets........................................................................................................181 ■ Chapter 7: Tuning Performance and Memory Usage........................................................................203 ■ Chapter 8: Versioning and Migrating Data ....................................................................................... 251 ■ Chapter 9: Using Core Data in Advanced Applications.....................................................................283 ■ Index:................................................................................................................................................359 Contents ■ About the Authors............................................................................................................................... xii ■ About the Technical Reviewer ........................................................................................................... xiii ■ Acknowledgments ............................................................................................................................. xiv ■ Introduction ....................................................................................................................................... xvi ■ Chapter 1: Getting Started .................................................................................................................... 1 What Is Core Data? ............................................................................................................................ 1 History of Persistence in iOS ............................................................................................................. 2 Creating a Basic Core Data Application............................................................................................. 3 Understanding the Core Data Components ................................................................................... 3 Creating a New Project ................................................................................................................. 5 Running Your New Project............................................................................................................6 Understanding the Application’s Components..............................................................................7 Fetching Results ...........................................................................................................................9 Inserting New Objects.................................................................................................................11 Initializing the Managed Context ................................................................................................ 13 Adding Core Data to an Existing Project.......................................................................................... 15 Adding the Core Data Framework...............................................................................................15 Creating the Data Model .............................................................................................................16 Initializing the Managed Object Context.....................................................................................21 Summary.......................................................................................................................................... 25 ■CONTENTS v ■CONTENTS ■ Chapter 2: Understanding Core Data .................................................................................................. 27 Core Data Framework Classes......................................................................................................... 27 The Model Definition Classes......................................................................................................30 The Data Access Classes.............................................................................................................38 Key-Value Observing...................................................................................................................42 The Query Classes.......................................................................................................................43 How the Classes Interact.................................................................................................................46 SQLite Primer .............................................................................................................................. 51 Reading the Data Using Core Data .............................................................................................. 53 Summary.......................................................................................................................................... 55 ■ Chapter 3: Storing Data: SQLite and Other Options ............................................................................ 57 Using SQLite as the Persistent Store ............................................................................................... 57 Configuring the One-to-Many Relationship.................................................................................61 Building the User Interface ......................................................................................................... 63 Configuring the Table..................................................................................................................66 Creating a Team..........................................................................................................................66 The Player User Interface............................................................................................................76 Adding, Editing, and Deleting Players.........................................................................................79 Seeing the Data in the Persistent Store......................................................................................85 Using an In-Memory Persistent Store ............................................................................................. 88 Creating Your Own Custom Persistent Store...................................................................................90 Initializing the Custom Store ......................................................................................................92 Mapping Between NSManagedObject and NSAtomicStoreCacheNode.......................................95 Serializing the Data.....................................................................................................................97 Using the Custom Store.............................................................................................................101 What About XML Persistent Stores?.........................................................................................103 Summary ................................................................................................................................... 106 ■ Chapter 4: Creating a Data Model..................................................................................................... 107 Designing Your Database...............................................................................................................107 Relational Database Normalization...........................................................................................108 Using the Xcode Data Modeler....................................................................................................... 109 Viewing and Editing Attribute Details.......................................................................................114 Viewing and Editing Relationship Details ................................................................................. 115 Using Fetched Properties..........................................................................................................116 Creating Entities ............................................................................................................................ 118 Creating Attributes ........................................................................................................................ 120 Creating Relationships................................................................................................................... 122 Name ......................................................................................................................................... 123 Optional ..................................................................................................................................... 124 Transient ................................................................................................................................... 124 Destination and Inverse ............................................................................................................ 124 To-Many Relationship ............................................................................................................... 125 Min Count and Max Count.........................................................................................................125 Delete Rule ................................................................................................................................ 125 Summary........................................................................................................................................ 126 ■ Chapter 5: Working with Data Objects ............................................................................................. 129 Understanding CRUD......................................................................................................................129 Creating the Shape Application Data Model ............................................................................. 132 Building the Shape Application User Interface.........................................................................138 Enabling User Interactions with the Shapes Application..........................................................149 Generating Classes ........................................................................................................................ 151 Modifying Generated Classes ........................................................................................................ 160 Using the Transformable Type.......................................................................................................165 Validating Data .............................................................................................................................. 168 Custom Validation.....................................................................................................................170 Invoking Validation ................................................................................................................... 174 Default Values ........................................................................................................................... 174 Undoing and Redoing.....................................................................................................................175 Undo Groups..............................................................................................................................176 Limiting the Undo Stack............................................................................................................176 ■CONTENTS vii ■CONTENTS viii Disabling Undo Tracking...........................................................................................................176 Adding Undo to Shapes.............................................................................................................177 Summary........................................................................................................................................ 180 ■ Chapter 6: Refining Result Sets........................................................................................................181 Building the Test Application......................................................................................................... 181 Creating the Org Chart Data......................................................................................................183 Reading and Outputting the Data..............................................................................................186 Filtering.......................................................................................................................................... 187 Expressions for a Single Value .................................................................................................188 Expressions for a Collection .....................................................................................................189 Comparison Predicates.............................................................................................................189 Compound Predicates...............................................................................................................192 Subqueries ................................................................................................................................ 194 Aggregating ................................................................................................................................... 197 Sorting ........................................................................................................................................... 199 Returning Unsorted Data...........................................................................................................199 Sorting Data on One Criterion ................................................................................................... 200 Sorting on Multiple Criteria ......................................................................................................201 Summary........................................................................................................................................ 202 ■ Chapter 7: Tuning Performance and Memory Usage........................................................................203 Building the Application for Testing .............................................................................................. 203 Creating the Core Data Project .................................................................................................204 Creating the Data Model and Data ............................................................................................ 206 Creating the Testing View.........................................................................................................208 Building the Testing Framework...............................................................................................211 Adding the Testing Framework to the Application ................................................................... 213 Running Your First Test ............................................................................................................215 Faulting .......................................................................................................................................... 218 Firing Faults..............................................................................................................................218 Faulting and Caching ................................................................................................................ 219 Refaulting .................................................................................................................................. 219 Building the Faulting Test ......................................................................................................... 220 Taking Control: Firing Faults on Purpose .................................................................................224 Prefetching................................................................................................................................ 225 Caching .......................................................................................................................................... 228 Expiring.......................................................................................................................................... 231 Memory Consumption...............................................................................................................232 Brute-Force Cache Expiration...................................................................................................232 Expiring the Cache Through Faulting........................................................................................232 Uniquing ......................................................................................................................................... 233 Improve Performance with Better Predicates...............................................................................237 Using Faster Comparators ........................................................................................................238 Using Subqueries ...................................................................................................................... 239 Analyzing Performance ................................................................................................................. 242 Launching Instruments ............................................................................................................. 243 Understanding the Results........................................................................................................246 Summary........................................................................................................................................ 248 ■ Chapter 8: Versioning and Migrating Data ....................................................................................... 251 Versioning ...................................................................................................................................... 252 Switching from Unversioned to Versioned ...............................................................................255 Lightweight Migrations.................................................................................................................. 255 Migrating a Simple Change.......................................................................................................256 Migrating More Complex Changes............................................................................................258 Renaming Entities and Properties ............................................................................................258 Creating a Mapping Model............................................................................................................. 261 Understanding Entity Mappings................................................................................................261 Understanding Property Mappings...........................................................................................263 Creating a New Model Version That Requires a Mapping Model..............................................264 Creating a Mapping Model........................................................................................................268 Migrating Data ............................................................................................................................... 275 ■CONTENTS ix ■CONTENTS Running Your Migration............................................................................................................276 Custom Migrations.........................................................................................................................279 Making Sure Migration Is Needed.............................................................................................279 Setting Up the Migration Manager............................................................................................280 Running the Migration .............................................................................................................. 280 Summary........................................................................................................................................ 281 ■ Chapter 9: Using Core Data in Advanced Applications.....................................................................283 Creating an Application for Note and Password Storage and Encryption.....................................283 Setting Up the Data Model ........................................................................................................284 Setting Up the Tab Bar Controller ............................................................................................. 287 Adding the Tab .......................................................................................................................... 291 Managing Table Views Using NSFetchedResultsController...........................................................297 Understanding NSFetchedResultsController.............................................................................298 The Fetch Request.....................................................................................................................298 The Managed Object Context .................................................................................................... 298 The Section Name Key Path ...................................................................................................... 299 The Cache Name........................................................................................................................ 299 Understanding NSFetchedResultsController Delegates............................................................299 Using NSFetchedResultsController ...........................................................................................300 Incorporating NSFetchedResultsController into MyStash ........................................................300 Creating the Fetched Results Controller ................................................................................... 302 Implementing the NSFetchedResultsControllerDelegate Protocol ........................................... 303 Incorporating the Fetched Results Controllers into the Tables ................................................ 305 Creating the Interface for Adding and Editing Notes and Passwords ...................................... 308 Splitting Data Across Multiple Persistent Stores .......................................................................... 323 Using Model Configurations......................................................................................................324 Adding Encryption ......................................................................................................................... 329 Persistent Store Encryption Using Data Protection .................................................................. 329 Data Encryption.........................................................................................................................332 Using Encryption.......................................................................................................................333 Automatically Encrypting Fields ............................................................................................... 334 Changing the User Interface to Use the text Attribute .............................................................. 335 Testing the Encryption .............................................................................................................. 338 Sending Notifications When Data Changes....................................................................................339 Registering an Observer ...........................................................................................................339 Receiving the Notifications.......................................................................................................340 Seeding Data..................................................................................................................................342 Adding Categories to Passwords..............................................................................................342 Creating a New Version of Seeded Data ...................................................................................345 Error Handling................................................................................................................................ 346 Handling Core Data Operational Errors.....................................................................................346 Handling Validation Errors........................................................................................................349 Handling Validation Errors in MyStash.....................................................................................352 Implementing the Validation Error Handling Routine ............................................................... 353 Summary ................................................................................................................................... 358 ■ Index.................................................................................................................................................359
用户评论