Core Data for Ios Developing Data-Driven Applications for the Ipad®, Iphone®, and Ipod Touch®
Total Page:16
File Type:pdf, Size:1020Kb
Tim Isted Tom Harrington Core Data for iOS Developing Data-Driven Applications for the iPad®, iPhone®, and iPod touch® Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City Many of the designations used by manufacturers and sellers to distinguish their Editor-in-Chief products are claimed as trademarks. Where those designations appear in this Mark Taub book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. Senior Acquisitions Editor The authors and publisher have taken care in the preparation of this book, but Chuck Toporek make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential Development Editor damages in connection with or arising out of the use of the information or Chuck Toporek programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for Managing Editor bulk purchases or special sales, which may include electronic versions and/or Kristy Hart custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: Project Editor Andy Beaster U.S. Corporate and Government Sales (800) 382-3419 Indexer [email protected] Larry Sweazy For sales outside the United States please contact: Proofreader International Sales Jennifer Gallant [email protected] Visit us on the Web: informit.com/aw Technical Reviewers Library of Congress Cataloging-in-Publication Data is on file. Jim Correia Copyright © 2011 Pearson Education, Inc. Robert McGovern All rights reserved. Printed in the United States of America. This publication is Mike Swan protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any Publishing Coordinator form or by any means, electronic, mechanical, photocopying, recording, or likewise. Olivia Basegio For information regarding permissions, write to: Interior Designer Pearson Education, Inc Gary Adair Rights and Contracts Department 501 Boylston Street, Suite 900 Cover Designer Boston, MA 02116 Chuti Prasertsith Fax (617) 671 3447 ISBN-13: 978-0-321-67042-7 Compositor ISBN-10: 0-321-67042-6 Gloria Schurick Text printed in the United States on recycled paper at R.R. Donnelley in Crawfordsville, Indiana First printing June 2011 iii Table of Contents 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 iv Core Data for iOS 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 Contents v 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