Learning Core Data for Ios with Swift Addison-Wesley Learning Series

Learning Core Data for Ios with Swift Addison-Wesley Learning Series

Learning Core Data for iOS with Swift Addison-Wesley Learning Series Visit informit.com/learningseries for a complete list of available publications. The Addison-Wesley Learning Series is a collection of hands-on program- ming guides that help you quickly learn a new technology or language so you can apply what you’ve learned right away. Each title comes with sample code for the application or applications built in the text. This code is fully annotated and can be reused in your own projects with no strings attached. Many chapters end with a series of exercises to encourage you to reexamine what you have just learned, and to tweak or adjust the code as a way of learning. Titles in this series take a simple approach: they get you going right away and leave you with the ability to walk off and build your own application and apply the language or technology to whatever you are working on. Learning Core Data for iOS with Swift Tim Roadley Boston • Columbus • Indianapolis • New York • San Francisco • Amsterdam • Cape Town Dubai • London • Madrid • Milan • Munich • Paris • Montreal • Toronto • Delhi Mexico City • São Paulo • Sidney • Hong Kong • Seoul • Singapore • Taipei • Tokyo 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 Trina MacDonald Learning Core Data for iOS with Swift is an independent publication and has not Senior Development Editor been authorized, sponsored, or otherwise approved by Apple Inc. Chris Zahn AirPlay, AirPort, AirPrint, AirTunes, App Store, Apple, the Apple logo, Apple TV, Development Editor Aqua, Bonjour, the Bonjour logo, Cocoa, Cocoa Touch, Cover Flow, Dashcode, Michael Thurston Finder, FireWire, iMac, Instruments, Interface Builder, iOS, iPad, iPhone, iPod, iPod touch, iTunes, the iTunes logo, Leopard, Mac, Mac logo, Macintosh, Multi- Managing Editor Touch, Objective-C, Quartz, QuickTime, QuickTime logo, Safari, Snow Leopard, Kristy Hart Spotlight, and Xcode are trademarks of Apple, Inc., registered in the United Project Editor States and other countries. OpenGL and the logo are registered trademarks of Elaine Wiley Silicon Graphics, Inc. The YouTube logo is a trademark of Google, Inc. Intel, Intel Core, and Xeon are trademarks of Intel Corp. in the United States and other Copy Editor countries. Geneil Breeze Proofreader The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility Leslie Joseph for errors or omissions. No liability is assumed for incidental or consequential Technical Reviewers damages in connection with or arising out of the use of the information or Carl Brown programs contained herein. Niklas Saers Ash Furrow The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or Publishing Coordinator custom covers and content particular to your business, training goals, marketing Olivia Basegio focus, and branding interests. For more information, please contact: Cover Designer U.S. Corporate and Government Sales Chuti Prasertsith (800) 382-3419 Compositor [email protected] Nonie Ratcliff For sales outside the United States, please contact: International Sales [email protected] Library of Congress Control Number: 2013946325 Visit us on the Web: informit.com/aw Copyright © 2016 Pearson Education, Inc. All rights reserved. Printed in the United States of America. This publication is 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 form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-134-12003-5 ISBN-10: 0-134-12003-4 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing: December 2015 ❖ The more I learn, the more I learn I need to learn more. I dedicate this book to my wonderful wife, Tracey, who has given up many nights and weekends with me to help make this book a reality. Finally, we can sit back and relax together again! I’m sure Tyler & Taliah will let us.... ❖ vi Contents Contents at a Glance Preface xv 1 Your First Core Data Application 1 2 Managed Object Model Basics 17 3 Managed Object Model Migration 43 4 Managed Object Model Expansion 73 5 Table Views 91 6 Views 121 7 Picker Views 151 8 Preloading Data 177 9 Deep Copy 201 10 Performance 221 11 Background Processing 249 12 Search 265 13 iCloud 271 14 Taming iCloud 283 15 CloudKit Sync: Uploading Objects 307 16 CloudKit Sync: Downloading Changes and Handling Deletions 337 A Preparing the Groceries Application 369 B Finalizing the Groceries Application 377 Contents vii Table of Contents Preface xv 1 Your First Core Data Application 1 What Is Core Data? 1 Persistent Store Coordinator 2 Managed Object Model 4 Managed Object Context 4 When to Use Core Data 5 Introducing the Groceries Application 5 Adding Core Data to an Existing Application 6 Implementing the Core Data Helper 7 Adding Paths 10 Adding the Core Data Stack 10 Adding the Setup Section 12 Adding the Saving Section 13 Adding a Managed Object Model File 14 Summary 15 Exercises 15 2 Managed Object Model Basics 17 What Is a Managed Object Model? 17 Entities 18 Attributes 20 Integer 16/32/64 21 Float and Double 22 Decimal 23 String 23 Boolean 23 Date 23 Binary Data 23 Transformable 24 Attribute Settings 25 Subclassing NSManagedObject 27 Scalar Properties for Primitive Data Types 28 Introducing the Demo Function 29 viii Contents Creating a Managed Object 29 Backend SQL Visibility 31 Fetching Managed Objects 34 Sorting Fetch Requests 36 Filtering Fetch Requests 36 Fetch Request Templates 38 Deleting Managed Objects 41 Summary 41 Exercises 42 3 Managed Object Model Migration 43 Changing a Managed Object Model 43 Adding a Model Version 45 Lightweight Migration 47 Default Migration 50 Migration Manager 54 Introducing MigrationVC.swift (Migration View Controller Code) 56 Introducing CDMigration.swift (Core Data Migration Code) 59 Summary 71 Exercises 71 4 Managed Object Model Expansion 73 Relationships 73 Delete Rules 79 Introducing CDOperation.swift (Core Data Operation) 80 Delete Rule Testing 83 Entity Inheritance 87 Summary 89 Exercises 89 5 Table Views 91 Table View Basics 91 Core Data Table Views 93 Introducing CDTableViewController 93 Expanding CDTableViewController 96 DATASOURCE: UITableView 98 Contents ix DELEGATE: NSFetchedResultsController 99 CDTableViewController Subclasses 101 Enhancing PrepareTVC 105 Preparing Test Data 107 Deletion and Cell Selection for PrepareTVC 109 Interaction for PrepareTVC 110 Introducing ShopTVC 114 Cell Selection for ShopTVC 116 INTERACTION for ShopTVC 116 Summary 118 Exercises 119 6 Views 121 View Basics 121 The Target View Hierarchy 122 Introducing ItemVC 123 Keeping Reference to a Selected Item 123 Passing a Selected Item to ItemVC 124 Configuring the Scroll View and Text Fields 125 Implementing ItemVC 129 DELEGATE: UITextField 130 The VIEW Section 131 Adding Functionality to NSManagedObject Subclasses 133 Units, Home Locations, and Shop Locations 136 Adding and Editing Units 138 Implementing UnitsTVC 141 Implementing UnitVC 143 Segueing from UnitsTVC to UnitVC 145 Adding and Editing Home or Shop Locations 146 Configuring the Home Location Views 147 Configuring the Shop Location Views 148 Summary 150 Exercises 150 x Contents 7 Picker Views 151 Picker View Basics 151 Introducing CDPickerTextField 152 Introducing UnitPickerTF 157 Creating the Unit Picker 159 Connecting the Unit Picker 160 Configuring ItemVC for the Unit Picker 161 Introducing LocationAtHomePickerTF 165 Introducing LocationAtShopPickerTF 167 Using the Location Pickers 168 Connecting the Location Pickers 169 Configuring ItemVC for the Location Pickers 170 Picker-Avoiding Text Field 173 Summary 176 Exercises 176 8 Preloading Data 177 Including Default Data 177 Indicating Whether an Import Is Required 178 Importing from XML 182 Creating an Import Context 184 Preventing Duplicate Default Data 185 Triggering a Default Data Import 186 Finding or Creating Managed Objects 188 Mapping XML Data to Entity Attributes 192 Importing from a Persistent Store 195 Using the Default Data Store as the Initial Store 196 Summary 199 Exercises 200 9 Deep Copy 201 The Deep Copy Process 201 Configuring a Source Stack 204 Configuring the Source Coordinator 204 Configuring the Source Context 205 Configuring the Source Store 205 Contents xi Enhancing CDImporter 206 Identifying Unique Attributes 207 Object Info 207 Copying a Unique Object 208 Establishing a To-One Relationship 210 Establishing a To-Many Relationship 211 Establishing an Ordered To-Many Relationship 212 Copying Relationships 213 Deep Copy Entities 215 Triggering a Deep Copy 216 Summary 219 Exercises 219 10 Performance 221 Identifying Performance Issues 221 Implementing the Camera 222 Implementing the Image Picker Controller Delegates 224 Generating Test Data 228 Merge Policies

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    51 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us