Pro Core Data for Ios Core Pro BLACK C 123 PANTONE YELLOW from Simple to Advanced Techniques
Total Page:16
File Type:pdf, Size:1020Kb
CYAN YELLOW SPOT MATTE MAGENTA BLACK PANTONE 123 C Covers ® Companion BOOKS FOR PROFESSIONALS BY PROFESSIONALS eBook iOS 4 Available he power of Core Data allows iOS developers to efficiently store and re- Pro Ttrieve application data using familiar object-oriented paradigms. Pro Core Store and retrieve your Apps data Data for iOS explains both how and why to use Core Data for data storage, from simple to advanced techniques. Covering common and advanced per- accurately and efficiently sistence patterns, this book prepares any iOS developer to store and retrieve data accurately and proficiently. Data Core Lots of iOS development books touch on Core Data, taking you through a few mainstream use cases for storing and retrieving data in your iOS applications. In Pro Core Data for iOS, however, we take you further into Core Data and show you how to leverage the power of this data framework. After reading this book, you’ll be able to answer all of these questions: • What are all the parts of Core Data, and how do they interact? • How do I create my own custom store? • Should I use plain NSManagedObject instances or custom classes? • How do I undo and redo Core Data actions? • How do I filter, sort, and aggregate data? for • What is “faulting,” and why should I care? iOS • Suppose I want to change my data model; how do I migrate my users’ data? Pro Core Data for iOS delves into these and other Core Data questions. With explanations, diagrams, code samples, and working explanations, this book will make you a Core Data pro! Pro Core Data for iOS Data Access and Persistence Engine for iPhone, iPad, and iPod touch Warner Privat Michael Privat | Robert Warner COMPANION eBOOK SEE LAST PAGE FOR DETAILS ON $10 eBOOK VERSION ISBN 978-1-4302-3355-8 53999 US $39.99 Shelve in Mobile Computing User level: www.apress.com Intermediate–Advanced 9 781430 233558 this print for content only—size & color not accurate Trim: 7.5 x 9.25 spine = 0.75" 400 page count 534ppi For your convenience Apress has placed some of the front This book was purchased by [email protected] matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. ■CONTENTS Contents at a Glance ■ About the Authors............................................................................................................................... xii ■ About the Technical Reviewer........................................................................................................... xiii ■ Acknowledgments ............................................................................................................................. xiv ■ Introduction ....................................................................................................................................... xvi ■ 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 iv ■INTRODUCTION Introduction Once you’ve learned the basics of iOS development and you’re ready to dig deeper into how to write great iOS applications, Pro Core Data for iOS leads you through the important topic of data persistence. Storing and retrieving customers’ data is a task you must pull off flawlessly for your application to survive and be used. Introductory texts give you introductory-level understanding of the Core Data framework, which is fine for introductory-level applications but not for applications that cross the chasm from toys to real-life, frequently used applications. This book provides you with the deeper levels of information and understanding necessary for developing killer apps that store and retrieve data with the performance, precision, and reliability customers expect and require. What to Expect from This Book This book starts by setting a clear foundation for what Core Data is and how it works and then takes you step-by-step through how to extract the results you need from this powerful framework. You’ll learn what the components of Core Data are and how they interact, how to design your data model, how to filter your results, how to tune performance, how to migrate your data across data model versions, and many other topics around and between these that will separate your apps from the crowd. This book combines theory and code to teach its subject matter. Although you can take the book to your Barcalounger and read it cover to cover, you’ll find the book is more effective if you’re in front of a computer, typing in and understanding the code it explains. We also hope that, after you read the book and work through its exercises, you’ll keep it handy as a reference, turning to it often for answers and clarification. How This Book Is Organized We’ve tried to arrange the material so that it grows in complexity, at least in a general sense, as the book progresses. The topics tend to build on each other, so you’ll likely benefit most by working through the book front to back, rather than skipping around. If you’re looking for guidance on a specific topic— xvi ■INTRODUCTION versioning and migrating data, say, or tuning performance and memory usage—skip ahead to that chapter. Most chapters focus on a single topic, indicated by that chapter’s title. The final chapter covers an array of advanced topics that didn’t fit neatly anywhere else. Source Code and Errata You can (and should!) download the source code from the Apress web site at www.apress.com. Feel free to use it in your own applications, whether personal or commercial. We tried to keep the text and code error-free, but some bug or typos might be unveiled over time. Corrections to both text and code can be found in this book’s errata section on the Apress web site. How to Contact Us We’d love to hear from you. Please send any questions or comments regarding this book or its accompanying source code to the authors. You can find them here: Michael Privat: E-mail: [email protected] Twitter: @michaelprivat Blog: http://michaelprivat.com Rob Warner: E-mail: [email protected] Twitter: @hoop33 Blog: http://grailbox.com xvii Chapter1 Getting Started If you misread this book’s title, thought it discussed and deciphered core dumps, and hope it will help you debug a nasty application crash, you got the wrong book. Get a debugger, memory tools, and an appointment with the optometrist. Otherwise, you bought, borrowed, burglarized, or acquired this book somehow because you want to better understand and implement Core Data in your iOS applications. You got the right book. You might read these words from a paper book, stout and sturdy and smelling faintly of binding glue. You might digitally flip through these pages on a nook, iPad, Kindle, Sony Reader, Kobo eReader, or some other electronic book reader. You might stare at a computer screen, whether on laptop, netbook, or monitor, reading a few words at a time while telling yourself to ignore your Twitter feed rolling CNN-like along the screen’s edge. Regardless, as you read, you know that not only can you stop at any time but that you can resume at any time. These words persist on paper and digital page and, with proper care and timely transformation to future media, can survive your grandchildren’s grandchildren. Any time you want to read this book, you pick up book, electronic reader, or keyboard, and if you marked the spot where you were last reading, you can even start from where you last stopped. We take this for granted with books. Users take it for granted with applications. Users expect to find their data each time they launch their applications. Apple’s Core Data framework helps you ensure that they will. This chapter introduces you to Core Data, explaining what it is, how it came to be, and how to build simple Core Data---based applications for iOS. This book walks through the simpleness and complexities of Core Data. Use the information in the book to create applications that store and retrieve data r e l ia b l y a n d e ffi c i e n t ly s o t h a t u se rs c an d e p e nd o n t h e i r d a ta . C od e c ar e fu l ly , t h o u gh ----- you don’t want to write buggy code and have to deal with nasty application crashes. What Is Core Data? When people use computers, they expect to preserve any progress they make toward completing their tasks. Saving progress, essential to office software,