ebook img

Professional iPhone and iPad database application programming PDF

383 Pages·2011·12.33 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Professional iPhone and iPad database application programming

PROFESSIONAL iPhone® and iPad™ Database Application Programming Patrick Alessi Professional iPhone® and iPad™ Database Application Programming Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2011 by Patrick Alessi Published by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-63617-6 ISBN: 978-1-118-00317-6 (ebk) ISBN: 978-1-118-00299-5 (ebk) ISBN: 978-1-118-00387-9 (ebk) Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at http://www.wiley.com/go/permissions. Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with respect to the accuracy or completeness of the contents of this work and specifi cally disclaim all warranties, including without limitation warranties of fi tness for a particular purpose. No warranty may be created or extended by sales or promotional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services. If professional assistance is required, the services of a competent professional person should be sought. Neither the publisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was written and when it is read. For general information on our other products and services please contact our Customer Care Department within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002. Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in electronic books. Library of Congress Control Number: 2010935566 Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affi liates, in the United States and other countries, and may not be used without written permission. iPhone and iPad are trademarks or registered trademarks of Apple, Inc. All other trademarks are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book. (cid:1) PART I M ANIPULATING AND DISPLAYING DATA ON THE IPHONE AND IPAD CHAPTER 1 Introducing Data-Driven Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 CHAPTER 2 The iPhone and iPad Database: SQLite . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 CHAPTER 3 Displaying Your Data: The UITableView . . . . . . . . . . . . . . . . . . . . . . . . . . 57 CHAPTER 4 iPad Interface Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89 (cid:1) PART II MANAGING YOUR DATA WITH CORE DATA CHAPTER 5 Introducing Core Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 CHAPTER 6 Modeling Data in Xcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 CHAPTER 7 Building a Core Data Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 CHAPTER 8 Core Data–Related Cocoa Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 CHAPTER 9 Core Data Migration and Performance . . . . . . . . . . . . . . . . . . . . . . . . . .235 (cid:1) PART III APPLICATION INTEGRATION USING WEB SERVICES CHAPTER 10 Working with XML on the iPhone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 CHAPTER 11 Integrating with Web Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301 APPENDIX A Tools for Troubleshooting Your Applications . . . . . . . . . . . . . . . . . . . . .343 INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355 PART I: M ANIPULATING AND DISPLAYING DATA ON THE IPHONE AND IPAD CHAPTER 1: INTRODUCING DATA-DRIVEN APPLICATIONS 3 Building a Simple Data-Driven Application 3 Creating the Project 4 Adding a UITableView 6 Model-View-Controller Architecture 6 Adding the TableView Programmatically 7 Retrieving Data 8 Implementing Your Data Model Class 9 Displaying the Data 12 Protocols 12 Implementing the UITableViewDataSource Protocol 13 Delegates 15 Finishing Up 15 Further Exploration 17 Design Patterns 17 Reading a Text File 18 Moving Forward 18 CHAPTER 2: THE IPHONE AND IPAD DATABASE: SQLITE 19 What Is SQLite? 20 The SQLite Library 20 SQLite and Core Data 20 Building a Simple Database 21 Designing the Database 22 Creating the Database 24 Populating the Database 26 Creating Records with the INSERT Command 26 Reading Your Rows with the SELECT Command 28 Tools to Visualize the SQLite Database 31 CONTENTS Connecting to Your Database 33 Starting the Project 34 The UINavigationController 34 The UITableViewController 35 The Model Class 36 The DBAccess Class 37 Parameterized Queries 47 Writing to the Database 49 Displaying the Catalog 50 Viewing Product Details 52 Moving Forward 55 CHAPTER 3: DISPLAYING YOUR DATA: THE UITABLEVIEW 57 Customizing the TableView 57 TableViewCell Styles 58 Adding Subviews to the contentView 61 Subclassing UITableViewCell 65 Getting Started 66 Implementing drawRect: 69 Finishing Up 71 Implementing Sections and an Index 72 Implementing Search 78 Optimizing TableView Performance 85 Reusing Existing Cells 85 Opaque Subviews 86 Custom Drawn Cells with drawRect 88 UI Conventions for Accessory Views 88 Moving Forward 88 CHAPTER 4: IPAD INTERFACE ELEMENTS 89 Displaying Master/Detail Data with the UISplitViewController 89 Introducing the UISplitViewController 90 The UISplitViewControllerDelegate Protocol 92 Starting the Split View Sample Application 93 Building the Detail Interface 95 Implementing Save and Master/Detail View 97 Setting Up the DetailViewController 97 Changes to the RootViewController 99 Modify the TableView Methods 100 Adding Surveys 101 CONTENTS Displaying Data in a Popover 103 Building the InfoViewController 104 Displaying the UIPopoverController 107 Gesture Recognizers 110 The UIGestureRecognizer Class 110 Using Gesture Recognizers 111 File Sharing Support 115 Enable File Sharing in the Sample Application 115 Serializing the Survey Data Array 115 Deserializing and Loading the Survey Data Array 117 Sharing the Data 118 Moving Forward 119 PART II: MANAGING YOUR DATA WITH CORE DATA CHAPTER 5: INTRODUCING CORE DATA 123 The Basics of Core Data 124 The Core Data Architecture 124 The Core Data Stack 124 The Data Store 125 The Persistent Store Coordinator 125 The Managed Object Model 126 The Managed Object Context 126 SQLite and Core Data 127 Using Core Data: A Simple Task Manager 127 Creating the Project 127 Examining the Template Code 128 TasksAppDelegate 128 The Data Model 131 RootViewController 132 Modifying the Template Code 137 Moving Forward 143 CHAPTER 6: MODELING DATA IN XCODE 145 Modeling Your Data 145 Defi ning Entities and Their Attributes 146 Entity Details 147 Adding Attributes 149 Adding Relationships Between Entities 150 CONTENTS Creating Fetched Properties and Fetch Request Templates 153 Fetched Properties 153 Fetch Request Templates 155 Creating Custom NSManagedObject Subclasses 155 Implementing Validation Rules 158 Implementing Default Values 159 Creating the Tasks Model 159 Moving Forward 161 CHAPTER 7: BUILDING A CORE DATA APPLICATION 163 The Tasks Application Architecture 163 The Data Model 164 The Class Model 164 The User Interface 165 Coding the Application 167 RootViewController and the Basic UI 167 Generating the Managed Object Subclasses 171 Adding and Viewing Tasks 172 Building the ViewTaskController 173 Changes to the RootViewController 178 Building the Editing Controllers 180 Editing Text with the EditTextController 181 Setting Priorities with the EditPriorityController 185 Adding and Editing Locations with the EditLocationController 189 Modifying Dates with the EditDateController 195 Finishing Up the Editing Controllers 199 Displaying Results in the RootViewController 203 Sorting Results with NSSortDescriptor 203 Filtering Results with NSPredicate 204 Generating Grouped Tables Using the NSFetchedResultsController 205 Implementing Custom Managed Objects 211 Coding a Dynamic Property 211 Defaulting Data at Runtime 212 Validating a Single Field 213 Multi-Field Validation 215 Moving Forward 217 CHAPTER 8: CORE DATA–RELATED COCOA FEATURES 219 Key-Value Coding 220 Keys and Keypaths 220 Setting Values Using Keys 221 CONTENTS Collection Operators 221 Additional Considerations When Using KVC 222 Key-Value Observing 222 Observing Changes to an Object 223 Automatic and Manual Implementations of KVO 224 Key-Value Observing Example 224 Building the User Interface 225 The Counter Data Object 226 Implementing Key-Value Observing 227 Updating Values with Key-Value Coding 229 Using NSPredicate 231 Creating Predicates 231 Using Predicates 232 Sort Descriptors 233 Moving Forward 234 CHAPTER 9: CORE DATA MIGRATION AND PERFORMANCE 235 Model Versioning and Schema Migration 235 Model Versioning 237 Lightweight Migration 239 Generating a Mapping Model 242 Safely Threading with Core Data 246 Designing for Threading 246 Threading and Core Data 247 Threading with NSOperation 248 Core Data Threading Example 249 Blocking the Main Thread 251 Moving the Blocking Call 252 Core Data Performance 257 Faulting 258 Data Store Types 259 Storing Binary Data 259 Entity Inheritance 260 Runtime Performance 260 Managing Changes with the Fetched Results Controller 261 Performance Analysis Using Instruments 264 Starting Instruments 264 The Instruments Interface 266 The Core Data Instruments 267 Moving Forward 268 CONTENTS PART III: APPLICATION INTEGRATION USING WEB SERVICES CHAPTER 10: WORKING WITH XML ON THE IPHONE 271 iPhone SDK and the Web 271 Web Application Architecture 272 Synchronous Data Retrieval 273 The URL Loading System 273 Web Access Sample 275 Starting the Application 275 Building the Interface 276 Requesting Data from the Server 277 Creating the Request 277 NSURLConnection Delegate Methods 278 Finishing Up 282 XML and the iPhone SDK 282 Brief Overview of XML 282 Parsing XML with NSXML Parser 283 Extending the Example, Parsing the XML 285 Starting Out 285 Setting Up to Parse 286 Modifying the Interface 288 Implementing the Parser Delegate Methods 288 Generating XML with libxml 291 XML Generation Sample 292 Moving Forward 300 CHAPTER 11: INTEGRATING WITH WEB SERVICES 301 Network Application Architecture 301 Two-Tier Architecture 302 Three-Tier Architecture (n-tier) 303 Application Communication 304 Introducing Web Services 304 SOAP Messaging 306 The REST Protocol 307 Example 1: Location-Based Search 308 Starting Out 308 Building the Interface 310 Core Location 310 The Core Location Framework 311 Using Core Location 312 CONTENTS The Local Search API 313 Using the Search Bar 315 Handling the Web Service Response 318 The NSURLConnection Delegate Methods 318 Defi ning the Result Class 320 Parsing the Response XML 322 Using MapKit 327 Finishing Up 331 Example 2: Term Extraction 332 Getting Started 332 Building the User Interface 334 Implementing the POST Call 335 Receiving the XML Response 338 Parsing the Response XML 340 Finishing Up 342 Moving Forward 342 APPENDIX A: TOOLS FOR TROUBLESHOOTING YOUR APPLICATIONS 343 Instruments 343 Starting Instruments 344 The Trace Document 345 Objective-C Memory Management 346 Sample Memory Leak Application 348 Analyzing a Memory Leak in Instruments 349 The Static Analyzer 351 INDEX 355

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.